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.
Older answer:
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.
I tend to use CIM Pings (Powershell v3+):
$Ping2 = Get-CimInstance -ClassName Win32_PingStatus -Filter "Address='$ComputerName' AND Timeout=1000";
Or WMI pings (Powershell v1 or v2):
$Ping = Get-WmiObject -Class Win32_PingStatus -Filter "Address='$ComputerName' AND Timeout=1000";
Either of which are basically the same, but return slightly different formats for things. Note that Get-WmiObject
is not available at all beginning in Powershell v6 because Get-CimInstance
was designed to supersede it.
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];
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.
9 Replies
-
Rob Dunn
This person is a verified professional.
Verify your account
to enable IT peers to see that you are a professional.pure capsaicin
test-connection actually establishes a connection to the WMI provider, so if it is having issues, you’ll need to focus on that — i.e. it essentially mimics the get-wmiobject -class win32_pingstatus class.
I would troubleshoot WMI on those workstations.
Was this post helpful?
thumb_up
thumb_down
-
Yeah I can do WMI queries on the machines in question….
Was this post helpful?
thumb_up
thumb_down
-
Rob Dunn
This person is a verified professional.
Verify your account
to enable IT peers to see that you are a professional.pure capsaicin
So you do not get anything like the following when you attempt this without the -QUIET switch — no errors, etc.?:
Powershell
Test-Connection -ComputerName ADMIN1
?
Text
Source Destination IPV4Address IPV6Address Bytes Time(ms) ------ ----------- ----------- ----------- ----- -------- ADMIN1 Server01 157.59.137.44 32 0 ADMIN1 Server01 157.59.137.44 32 0 ADMIN1 Server01 157.59.137.44 32 0 ADMIN1 Server01 157.59.137.44 32 1
Was this post helpful?
thumb_up
thumb_down
-
Rob Dunn
This person is a verified professional.
Verify your account
to enable IT peers to see that you are a professional.pure capsaicin
Can you share the exact cmdlet syntax you are using?
Was this post helpful?
thumb_up
thumb_down
-
I do get results but only 2/4 the other two give the error «failed due to lack of resources» What I’m trying to do is resolve host names from a list of IP’s. I figured first I would need to test to make sure the PC is on then use [System.Net.DNS] to resolve the name. Maybe there’s an easier way to do it in Powershell V.2?
Was this post helpful?
thumb_up
thumb_down
-
Powershell
Test-Connection -computername 10.10.45.94
This is the error
Powershell
Test-Connection : Testing connection to computer '10.10.45.94' failed: Error due to lack of resources At line:1 char:1 + Test-Connection -ComputerName 10.10.45.94 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ResourceUnavailable: (10.10.45.94:String) [Test-Connection], PingException + FullyQualifiedErrorId : TestConnectionException,Microsoft.PowerShell.Commands.TestConnectionCommand Source Destination IPV4Address IPV6Address Bytes Time(ms) ------ ----------- ----------- ----------- ----- -------- TECH02 10.10.45.94 10.10.45.94 32 31 TECH02 10.10.45.94 10.10.45.94 32 15 TECH02 10.10.45.94 10.10.45.94 32 15
Was this post helpful?
thumb_up
thumb_down
-
Rob Dunn
This person is a verified professional.
Verify your account
to enable IT peers to see that you are a professional.pure capsaicin
What type of device is at that IP address that fails?
In any case, I think there is a bug with Test-Connection in PowerShell 2.0. I’m testing now, but I would simply update your system to PS 4.0 if you can and run it from there.
Here’s some details, while not directly the same issue that you have, it does highlight a problem with the error handling of test-connection in 2.0:
http://blog.powershell.no/2011/10/23/test-connection-error-handling-gotcha-in-powershell-2-0/ Opens a new window
Was this post helpful?
thumb_up
thumb_down
-
Sorry to clarify I’m running V.3, but I’m running it against v.2 PC’s. Thanks for the info.
Was this post helpful?
thumb_up
thumb_down
-
I had same problem. Only what was necessary to do was enable ping in Firewall.
Was this post helpful?
thumb_up
thumb_down
- Remove From My Forums
-
Question
-
Hi!
I use Test-Connection Cmdlet to ping a lot of PCs with workflow script (throttlelimit 5).
Sometimes I get error message:
Test-Connect error: Error due to lack of resources.
When I use same script without workflow (10 copies of ps1-file) at the same PC, I don’t get error message.
Powershell 4.0
Windows 7 SP1 Enterprise, installed all updates.
Intel Pentium Dual CPU E2180 @ 2.00 GHz
Memory — 4 GB
Pagefile is enabled.
Answers
-
Start like this and return all data then insert into database.
workflow PrinterBase{ $Computerlist = Get-Content C:\Powershell\PrinterBase\ComputerList.txt foreach -parallel -throttlelimit 10 ($Computer in $Computerlist){ inlinescript{ $props=@{ Status='Unavailablle' HostName=$Using:Computer HostIp=$null HostPrefix=$null HostUserName=$null Printers=@() CurrentTime= Get-Date } if (test-connection -ComputerName $Using:Computer -count 1 -quiet){ $props.Status='Connected' Try{ $PrinterHost = Get-WMIObject -ComputerName $Using:Computer -class Win32_ComputerSystem -ErrorAction stop $props.HostUserName = $PrinterHost.UserName $NetworkInfo = Get-WmiObject -ComputerName $Using:Computer -Class Win32_NetworkAdapterConfiguration -Filter 'IPEnabled=True' -ErrorAction stop Foreach ($NetworkAdapter in $NetworkInfo){ $NetworkAdapterCaption = $NetworkAdapter.Caption } $printers = Get-WmiObject -ComputerName $Using:Computer -class Win32_Printer -ErrorAction stop $props.Printers=foreach ($printer in $Printers){ [pscustomobject]@{ PrinterCaption = $Printer.Name PrinterDriverName = $Printer.DriverName } } } Catch{ $props.Status="$_" } } New-Object PsObject -Property $props } }
This will reduce resource usage on the local system dramatically and will be much faster.
¯\_(ツ)_/¯
-
Edited by
Monday, March 2, 2015 8:02 PM
-
Marked as answer by
Sergey Vasiltsov
Tuesday, March 3, 2015 6:31 AM
-
Edited by
Solution 1
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.
Older answer:
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.
I tend to use CIM Pings (Powershell v3+):
$Ping2 = Get-CimInstance -ClassName Win32_PingStatus -Filter "Address='$ComputerName' AND Timeout=1000";
Or WMI pings (Powershell v1 or v2):
$Ping = Get-WmiObject -Class Win32_PingStatus -Filter "Address='$ComputerName' AND Timeout=1000";
Either of which are basically the same, but return slightly different formats for things. Note that Get-WmiObject
is not available at all beginning in Powershell v6 because Get-CimInstance
was designed to supersede it.
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];
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.
Solution 2
I am partial to using the .Net Ping class rather than Test-Connection
$Timeout = 100
$Ping = New-Object System.Net.NetworkInformation.Ping
$Response = $Ping.Send($Name,$Timeout)
$Response.Status
Note that the Send method can take additional parameters if you need to set TTL/Fragmentation. Also timeout is in milliseconds, with just $name the timeout I think is 5 seconds, which is usually too long.
Solution 3
Windows IP Helper defines IP_REQ_TIMED_OUT error to value 11010 wich is the same as Windows system error WSA_QOS_ADMISSION_FAILURE 11010 ‘Error due to lack of resources.’
so it is likely that what actually received in questioned case was time out error and simply misinterpreted as ‘lack of resources’.
Related videos on Youtube
14 : 45
PowerShell Tutorial 10 : Error Handling [Beginners]
02 : 28
Fix: Failed to load resources the server responded with a filename status of 404 | Web API ASPNET
40 : 12
Automate your load testing using Azure DevOps, K6 and Log Analytics
01 : 48
Powershell denied access — how to fix it!
16 : 01
PowerShell S2E64 (Pending reboots)
IT Generalist aka Mr Automation
14 : 34
Use PowerShell to reset computer trusts
27 : 49
PowerShell Errors and Exceptions Handling
06 : 52
Using Try/Catch Blocks In PowerShell
18 : 48
Handling Errors in PowerShell with Try..Catch..Finally
05 : 07
Network Troubleshooting with PowerShell
46 : 13
Top 10 PowerShell Commands for Troubleshooting
02 : 56
nodemon not run in Terminal | PowerShell Execution Policy Restricted | Technical Hassan Ali
01 : 04 : 08
Troubleshooting Windows Problems using PowerShell | Guy Leech | PSDayUK 2019
Comments
-
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?
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} }
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.
-
well, that looks pretty weird, so to work around that you can implement
do-while
loop, but I suggest you fight the root cause. Like what did you try to fix it?/sfc scannow
at least? anything? Upgrade to PS5? -
Have actually seen this before but was never able to locate a fix, although iirc a reboot would generally correct it(I could be wrong, has been over a year) Also what version of PS are you running? seem to remember the issue only impacting PSv2(again, could be misremembering)
-
How about using
-Quiet
or-ErrorAction SilentlyContinue
or both? This may be caused by aWMI
failure on the remote host. And-Count 1
is not always reliable. My usual line:if(Test-Connection $host -Quiet -Count 2 -EA 0) { #... }
, works like a charm. -
I’m using Powershell 5 on Windows 10 most often, but it occurs on Powershell 4 on 2012 R2 too. I haven’t extensively tested other versions beyond those. I can silently ignore the errors but the errors occur both on hosts that are up and ones that aren’t, so the results are inaccurate either way. Restarting Powershell and restarting the computer don’t fix it (or don’t fix it for long), and are not viable options.
-
-
It’s likely due to the fact that the .NET pings don’t return verbose errors, it appears to be mostly Success or TimedOut, though I haven’t tested with a flaky network to see if it returns some of the other error codes.
-
@dragon788 Yes, most of those network states are very difficult to recreate easily.
-
This makes more sense than a DNS issue, especially when testing against successful and failing IPv4 addressing like 8.8.8.8 and 7.7.7.7
-
The
Get-WmiObject
version does not work with IPv6 addresss. According to Microsoft, IPv6 is supported in WMI since Visa. However, using an IP6 addresses produces an error where WMI complains about an invalid:
character.Get-CimInstance
works as expected for both IPv4 and IPv6. -
I like wmi ping.
Recents
Test-Connection -ComputerName testserver.example.com -Count 1
Test-Connection : Provider load failure
At line:1 char:1
+ Test-Connection -ComputerName testserver.example.com -Count 1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Test-Connection], ManagementException
+ FullyQualifiedErrorId : TestConnectionException,Microsoft.PowerShell.Commands.TestConnectionCommand
I removed -Count 1 then i got this
PS C:\Output_Monitors> Test-Connection -ComputerName testserver.example.com
Test-Connection :
At line:1 char:1
+ Test-Connection -ComputerName testserver.example.com
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Test-Connection], COMException
+ FullyQualifiedErrorId : TestConnectionException,Microsoft.PowerShell.Commands.TestConnectionCommand
Then i did like this removing -ComputerName
PS C:\Output_Monitors> Test-Connection testserver.example.com
Test-Connection : Provider load failure
At line:1 char:1
+ Test-Connection testserver.example.com
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Test-Connection], ManagementException
+ FullyQualifiedErrorId : TestConnectionException,Microsoft.PowerShell.Commands.TestConnectionCommand