gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 13:23:51 26 4
gpt4 key购买 nike

测试连接间歇性失败,并出现资源不足错误:

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

最佳答案

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

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

但是,当网络完全不可用时,我最近尚未进行过测试。

较旧的答案:

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

我倾向于使用WMI ping:
$Ping = Get-WmiObject -Class Win32_PingStatus -Filter "Address='$ComputerName' AND Timeout=1000";

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

两者基本相同,但返回的格式略有不同。这里的主要缺点是您必须自己解析状态代码:
$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];

另外,我也使用过类似@BenH描述的.Net Pings,它为您完成了很多工作。我停止使用它们来支持WMI和CIM是有原因的,但是我已经不记得那个原因了。

关于powershell - Powershell-由于缺乏资源,测试连接失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41267553/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com