首先感谢您的阅读。我是 Powershell 的新手,所以请原谅我的无知。另外,这是我第一次在这里发帖。
然而,在我拥有它并且它正在工作之前,它非常缓慢。这是初始脚本:
$RemoteComputers = (Get-ADComputer -Filter {OperatingSystem -Like 'Windows*'} -Property * -SearchBase "OU=Computers,OU=Domain".Name
ForEach ($Computer in $RemoteComputers)
{
$result = Invoke-Command -ErrorAction SilentlyContinue -ComputerName $Computer -ScriptBlock {Test-NetConnection -Port 135 server.domain.local}
[pscustomobject]@{
Target = $Computer
RemoteAddress = $result.RemoteAddress
SourceAddress = (Resolve-Dnsname $Computer -ErrorAction SilentlyContinue).IPAddress
Port = $result.RemotePort
Status = $result.tcpTestSucceeded
}
}
然后通过这样做来运行它
.\ports.ps1 | Tee-Object -Variable result | Export-Csv ports-all.csv -NoTypeInformation
所以下面我删除了 -Property * 并清理了它。但是 CSV 文件中没有内容。谁能告诉我为什么当我运行它时 csv 文件中没有内容?
$RemoteComputers = (Get-ADComputer -Filter {OperatingSystem -Like 'Windows*'} -SearchBase "OU=Computers,OU=Domain").Name
$results = try {
Invoke-Command -ErrorAction Stop -ComputerName $RemoteComputers -ScriptBlock {
$Test = Test-NetConnection -Port 135 "server.domain.local"
[pscustomobject]@{
Target = $Env:COMPUTERNAME
RemoteAddress = $Test.RemoteAddress
SourceAddress = (Resolve-Dnsname $Env:COMPUTERNAME -ErrorAction SilentlyContinue).IPAddress
Port = $Test.RemotePort
Status = $Test.tcpTestSucceeded
}
}
}
catch {
Write-Output ([pscustomobject]@{
Target = $_.TargetObject
RemoteAddress = $null
SourceAddress = $null
Port = $null
Status = "Failed to connect"
})
}
$results | select target, remoteaddress, sourceaddress, port, status | export-csv file.csv
所以最后一个脚本不会向 CSV 输出任何内容。为什么?