Powershell-由于缺少资源,测试连接失败 [英] Powershell - Test-Connection failed due to lack of resources

查看:426
本文介绍了Powershell-由于缺少资源,测试连接失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Test-connection intermittently fails with a lack of resources error:

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

因此,当您需要循环测试计算机列表时,它是不可靠且相当无用的.是否有修复程序,替代方法或替代方法来可靠地获得此功能?

As a result, it's not reliable and fairly useless when you need to test a list of computers in a loop. Is there a fix, alternative, or workaround to get this functionality reliably?

这是我当前的解决方案,但是它仍然不够可靠(有时它们仍然连续5次失败),并且由于所有延迟和重试而需要花费很多时间.

This is my current solution, but it's still not sufficiently reliable (sometimes they still fail 5 times in a row) and it takes forever because of all the delays and retries.

$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响应的状态)进行解析,那似乎是理想的解决方案,但是我只需要一些东西可行,所以我愿意接受想法.

A normal ping (ping.exe) works every time, so if there's a good way to parse that with powershell (host up or down, what IP is responding), that seems like the ideal solution, but I just need something that works, so I'm open to ideas.

推荐答案

在PowerShell的较新版本中,Test-Connection上的-Quiet参数似乎总是返回TrueFalse.在旧版本上似乎无法始终如一地工作,但是要么我现在做的事情有所不同,要么他们已经对其进行了改进:

In newer versions of PowerShell, the -Quiet parameter on Test-Connection does seem to always return either True or False. It didn't seem to work consistently on older versions, but either I'm doing something differently now or they've improved it:

$Ping = Test-Connection -ComputerName $ComputerName -Count 1 -Quiet

但是,当网络根本不可用时,我最近还没有对其进行测试.

I haven't tested it recently when the network is simply unavailable, however.

较旧的答案:

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

Test-Connection doesn't respond well when DNS doesn't respond with an address or when the network is unavailable. That is, if the cmdlet decides it can't send the ping at all, it errors in unpleasant ways that are difficult to trap or ignore. Test-Connection is only useful, then, when you can guarantee that DNS will resolve the name to an address, and that the network will always be present.

我倾向于使用WMI ping:

I tend to use WMI pings:

$Ping = Get-WmiObject -Class Win32_PingStatus -Filter "Address='$ComputerName' AND Timeout=1000";

或CIM Pings:

Or CIM Pings:

$Ping2 = Get-CimInstance -ClassName Win32_PingStatus -Filter "Address='$ComputerName' AND Timeout=1000";

两者基本上相同,但是返回的格式略有不同.这里的主要缺点是您必须自己解析状态代码:

Either of which are basically the same, but return slightly different formats for things. The main disadvantage here is that you have to resolve the status code yourself:

$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];

或者,我也使用过.Net Ping,如@BenH描述的,它为您完成了很多工作.我之所以停止使用它们来支持WMI和CIM是有原因的,但是我已经不记得那个原因了.

Alternately, I've used .Net Pings like @BenH described, too, which does a lot of that work for you. There was a reason I stopped using them in favor of WMI and CIM, but I can no longer remember what that reason was.

这篇关于Powershell-由于缺少资源,测试连接失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆