问题描述
我有一个脚本,该脚本从计算机列表中读取并进行测试连接.如果它从Test-Connection
得到答复,则执行if/else,并从Get-ChildItem
构建一个变量,该变量从可疑系统C:\Users
中读取,以查看最后一个登录的人并选择名称并最后写入最后一个登录用户的时间,并对该结果执行Write-Host
.如果Test-Conneciton
没有得到答复,它将使用"offline"语句构建系统名称的变量,并执行Write-Host
来显示它.然后,要完成所有操作,它将结果导出到CSV.
I have a script that reads from a list of computers, does a test connection. If it gets a reply from the Test-Connection
, it does a if/else and builds a variable from a Get-ChildItem
that reads from the suspect system C:\Users
to see the last person to log in and select the name and last write time of the last user that logged in, and does a Write-Host
of the results. If the Test-Conneciton
didn't get a reply, it builds a variable of the system name with a "offline" statement and does a Write-Host
to show it. Then, to finish it all off, it exports the results to a CSV.
我遇到一些问题:
-
Get-ChildItem
的变量执行Select-Object Name, LastWriteTime
,并将结果构建为变量.我想单独显示这些结果(当然也将它们导出到CSV中),而不是合并显示. - 我需要修剪结果或修改脚本,以便在结果中不包含多余的字符.
- The variable for the
Get-ChildItem
does aSelect-Object Name, LastWriteTime
and builds the results into a variable. I would like to display those results (and of course export them into the CSV) separately instead of combined. - I need to trim the results or modify the script so it doesn't include extra characters in the results.
这里是一个示例,当Test-Connection
从计算机获得答复时,Get-ChildItem的结果(当然,系统名称,IP地址和用户名已更改为保护无辜者):
Here is an example of the results from the Get-ChildItem when the Test-Connection
gets a reply from the machine (of course system name, IP address and user name have been changed to protect the innocent):
SYSTEMNAME @{IPV4Address=192.168.0.1} @{Name=John.Doe; LastWriteTime=03/08/2017 08:11:48}
名称和上次写入时间组合在结果中,我需要在显示和CSV导出中将它们分开,并修剪多余的字符.这样的话,系统名称,IP地址,名称和lastwritetime都将出现在CSV自身的单元格中:
The name and last write time are combined in the results and I need to split them out in the display and the CSV export as well as trim the extra characters. Something like this, where systemname, IP address, name and lastwritetime would all appear in their own cells in the CSV:
SYSTEMNAME 192.168.0.1 Name=John.Doe LastWriteTime=03/08/2017 08:11:48
下面的代码:
$computerList = Get-Content "D:\filelocation\LastLogIn.txt"
foreach ($Computername in $computerList) {
$ipreachable = Test-Connection $computerName -EA SilentlyContinue -Count 1 |
select IPV4address
$output =@()
if ($ipreachable) {
$LastUserLoggedIn = Get-ChildItem "\\$computername\c$\Users" -EA SilentlyContinue |
Sort-Object LastWriteTime -Descending |
Select-Object Name, LastWriteTime -First 1
$Details = "$LastUserLoggedIn"
Write-Host $computername $ipreachable $Details
} else {
$details = "$computerName Computer does not exisit or is offline"
Write-Host $Details
}
[PSCustomObject]@{
SystemName = $Computername
IPV4Address = $ipreachable
UserLogInDetails = $details
} | Export-Csv "D:\filelocation\lastuserreults.csv" -NoType -Append
}
推荐答案
扩展IP地址:
... | Select-Object -Expand IPV4Address | Select-Object -Expand IPAddressToString
不要将目录信息转换为字符串:
and don't turn the directory information into a string:
像这样构建数据对象:
$obj = [PSCustomObject]@{
SystemName = $Computername
IPV4Address = $ipreachable
UserName = $LastUserLoggedIn.Name
Timestamp = $LastUserLoggedIn.LastWriteTime
}
这样您就可以在控制台上显示信息并将其导出为CSV:
So that you can display the information on the console as well as export it to a CSV:
$obj | Format-Table -AutoSize
$obj | Export-Csv 'C:\path\to\your.csv' -NoType -Append
这篇关于选择对象选择多个对象,但拆分结果并对其进行修剪,将其导出为CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!