我有一个脚本,在该脚本下ping机器列表,将结果输出到CSV并获取机器的lastlogontimestamp。
工作正常,除了lastlogontimestamp这样显示:
CCC-APP01,172.22.100.15,@{lastLogonDate=07/25/2018 13:24:54}
如何摆脱多余的字符: @ {lastlogondate = ...} ?
$OutputCSV = "C:\TEMP\OUPingResults.csv"
$SearchLocation = "OU=AA,OU=Servers,DC=LocA,DC=XYZ,DC=com"
$Computers = Get-ADComputer -Filter * -SearchBase $SearchLocation |
Select Name |
Sort-Object Name
$Computers = $Computers.Name
$Headers = "ComputerName,IP Address,LastLogonTimeStamp"
$Headers | Out-File -FilePath $OutputCSV -Encoding UTF8
foreach ($computer in $Computers) {
Write-host "Pinging $Computer"
$Test = Test-Connection -ComputerName $computer -Count 1 -ErrorAction SilentlyContinue -ErrorVariable Err
if ($test -ne $null) {
$IP = $Test.IPV4Address.IPAddressToString
$LastLogonTimeStamp = Get-ADComputer $Computer -Prop CN,lastLogonTimestamp |
Select @{n="lastLogonDate";e={[datetime]::FromFileTime($_.lastLogonTimestamp)}}
$Output = "$Computer,$IP,$LastLogonTimeStamp"
$Output | Out-File -FilePath $OutputCSV -Encoding UTF8 -Append
} else {
$Output = "$Computer,$Err"
$Output | Out-File -FilePath $OutputCSV -Encoding UTF8 -Append
}
}
答案 0 :(得分:3)
表达式... |Select-Object @{N='SomeName';E={"SomeValue"}}
将产生一个对象,该对象具有名为SomeName
且值为"SomeValue"
的属性。
在输出中看到的是该对象的字符串表示形式。
如果只需要该值,请将$LastLogonTimeStamp
分配更改为:
$LastLogonTimeStamp = [datetime]::FromFiletime((Get-ADComputer $Computer -Prop lastLogonTimestamp).lastLogonTimestamp)