Powershell - 由于缺乏资源,测试连接失败

csc*_*ker 18 powershell ping

由于缺少资源错误,测试连接会间歇性地失败:

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
Run Code Online (Sandbox Code Playgroud)

因此,当您需要在循环中测试计算机列表时,它不可靠且相当无用.是否有可靠的修复,替代或解决方法来实现此功能?

这是我目前的解决方案,但它仍然不够可靠(有时它们仍然连续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}
}
Run Code Online (Sandbox Code Playgroud)

普通ping(ping.exe)每次都有效,所以如果有一个很好的方法来解析PowerShell(主机向上或向下,IP响应),这似乎是理想的解决方案,但我只需要一些有用的东西,所以我愿意接受各种想法.

Bac*_*its 14

在较新版本的PowerShell中,-Quiet参数on Test-Connection似乎总是返回TrueFalse.它似乎在旧版本上并不一致,但要么我现在做了不同的事情,要么他们改进了它:

$Ping = Test-Connection -ComputerName $ComputerName -Count 1 -Quiet
Run Code Online (Sandbox Code Playgroud)

但是,当网络根本不可用时,我最近没有测试过它.


老答案:

Test-Connection当DNS没有响应地址或网络不可用时,响应不佳.也就是说,如果cmdlet决定它根本无法发送ping,则会以难以捕获或忽略的令人不快的方式发生错误. Test-Connection只有在您可以保证DNS将名称解析为地址并且网络始终存在时才有用.

我倾向于使用WMI ping:

$Ping = Get-WmiObject -Class Win32_PingStatus -Filter "Address='$ComputerName' AND Timeout=1000";
Run Code Online (Sandbox Code Playgroud)

或CIM Pings:

$Ping2 = Get-CimInstance -ClassName Win32_PingStatus -Filter "Address='$ComputerName' AND Timeout=1000";
Run Code Online (Sandbox Code Playgroud)

其中任何一个都基本相同,但返回的格式略有不同.这里的主要缺点是您必须自己解决状态代码:

$StatusCodes = @{
    [uint32]0     = 'Success';
    [uint32]11001 = 'Buffer Too Small';
    [uint32]11002 = 'Destination Net Unreachable';
    [uint32]11003 = 'Destination Host Unreachable';
    [uint32]11004 = 'Destination Protocol Unreachable';
    [uint32]11005 = 'Destination Port Unreachable';
    [uint32]11006 = 'No Resources';
    [uint32]11007 = 'Bad Option';
    [uint32]11008 = 'Hardware Error';
    [uint32]11009 = 'Packet Too Big';
    [uint32]11010 = 'Request Timed Out';
    [uint32]11011 = 'Bad Request';
    [uint32]11012 = 'Bad Route';
    [uint32]11013 = 'TimeToLive Expired Transit';
    [uint32]11014 = 'TimeToLive Expired Reassembly';
    [uint32]11015 = 'Parameter Problem';
    [uint32]11016 = 'Source Quench';
    [uint32]11017 = 'Option Too Big';
    [uint32]11018 = 'Bad Destination';
    [uint32]11032 = 'Negotiating IPSEC';
    [uint32]11050 = 'General Failure'
    };
$StatusCodes[$Ping.StatusCode];
$StatusCodes[$Ping2.StatusCode];
Run Code Online (Sandbox Code Playgroud)

或者,我也使用了像@BenH这样的.Net Pings,它为你做了很多工作.有一个原因我停止使用它们支持WMI和CIM,但我不能再记得那个原因了.


Ben*_*enH 12

我偏爱使用.Net Ping类而不是Test-Connection

$Timeout = 100
$Ping = New-Object System.Net.NetworkInformation.Ping
$Response = $Ping.Send($Name,$Timeout)
$Response.Status
Run Code Online (Sandbox Code Playgroud)

请注意,如果需要设置TTL/Fragmentation,Send方法可以采用其他参数.此外,超时是以毫秒为单位,只有$ name超时我认为是5秒,这通常太长.


wto*_*tom 7

Windows IP Helper将IP_REQ_TIMED_OUT错误定义为值11010,与Windows系统错误WSA_QOS_ADMISSION_FAILURE 11010“由于缺少资源而导致的错误”相同。因此,在有疑问的情况下,实际收到的可能是超时错误,并被误解为“资源不足”。