测试连接间歇性失败,提示资源不足错误:
test-connection : Testing connection to computer 'SOMESERVER' failed: Error due to lack of resources
At line:1 char:45
+ ... ($server in $ServersNonProd.Name) { test-connection $server -Count 1}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (SOMESERVER:String) [Test-Connection], PingException
+ FullyQualifiedErrorId : TestConnectionException,Microsoft.PowerShell.Commands.TestConnectionCommand
因此,在需要在循环中测试计算机列表时,它并不可靠且几乎没有用。是否有修复、替代方案或解决方法可以使此功能可靠?
这是我目前的解决方案,但仍然不够可靠(有时它们仍然会连续失败5次),而且由于所有延迟和重试,它需要很长时间。
$Servers = Import-CSV -Path C:\Temp\Servers.csv
$result = foreach ($Name in $Servers.FQDN) {
$IP = $null
if ( Resolve-DNSName $Name -ErrorAction SilentlyContinue ) {
$IP = (Test-Connection -Count 1 -ComputerName $Name -ErrorAction SilentlyContinue).IPv4Address
if ( $IP -eq $null ) {
Start-Sleep -Milliseconds 100
$IP = (Test-Connection -Count 1 -ComputerName $Name -ErrorAction SilentlyContinue).IPv4Address
}
if ( $IP -eq $null ) {
Start-Sleep -Milliseconds 200
$IP = (Test-Connection -Count 1 -ComputerName $Name -ErrorAction SilentlyContinue).IPv4Address
}
if ( $IP -eq $null ) {
Start-Sleep -Milliseconds 300
$IP = (Test-Connection -Count 1 -ComputerName $Name -ErrorAction SilentlyContinue).IPv4Address
}
if ( $IP -eq $null ) {
Start-Sleep -Milliseconds 400
$IP = (Test-Connection -Count 1 -ComputerName $Name -ErrorAction SilentlyContinue).IPv4Address
}
}
new-object psobject -Property @{FQDN = $Name; "IP Address" = $IP}
}
一般的ping(ping.exe)每次都有效,因此如果有一种好的方法可以用powershell解析它(主机是正常的还是宕机了,哪个IP地址正在响应),那似乎是最理想的解决方案,但我只需要能够工作的东西,所以我愿意接受任何建议。
/sfc scannow
命令吗?还是升级到PS5? - 4c74356b41-Quiet
或-ErrorAction SilentlyContinue
或两者都使用如何?这可能是由于远程主机上的WMI
故障引起的。而-Count 1
并不总是可靠的。我的常规代码:if(Test-Connection $host -Quiet -Count 2 -EA 0) { #... }
,非常好用。 - sodawillow