test-connection

rated by 0 users
This post has 7 Replies | 5 Followers

Top 200 Contributor
Posts 8
patg Posted: 01-10-2012 10:29 AM

Normal 0 false false false EN-US X-NONE X-NONE MicrosoftInternetExplorer4

don - in your Month of Lunches book chapter 9.8 you have ideas for on your own and ask:

Or use Test-Connection to ping several

computers, and only show the results from computers that did not respond

 

I tried several things then did what I do best and stole a cmd and came up with this (I am sure you know where I stole it from…J)

get-content c:\hold\machines.txt | foreach { if (test-connection $_ -quiet) { $null } else {write-host "$_ is down" -foregroundcolor RED}}

 

Now I get the desired results but my question is :       how does it know if it’s up or down?  All I did was run a test-connection and then a write-host.   I didn’t call for anything like statuscode or timeout and say if statuscode is X  then …….

Also if I ping a computer (who is in dns but not up) I get request timed out…..but test-connection gives me:

Test-Connection : Testing connection to computer 'XXXX' failed: The requested name is valid, but no data of the requested type was found At line:1 char:16 + test-connection <<<<  XXXX     + CategoryInfo          : ResourceUnavailable: (XXXX:String) [Test-Connection], PingException      + FullyQualifiedErrorId : TestConnectionException,Microsoft.PowerShell.Commands.TestConnectionCommand

Top 10 Contributor
Posts 640

I'm not sure I know what the question is :) but keep in mind that Test-Connection doesn't use the normal Ping.exe under the hood. It uses the Win32_PingStatus WMI class from the local machine. Also, when you use the -quiet switch you're ONLY getting a True/False - you essentially "lose" the machine name. It's normal to get an error from that class when it runs into that situation and you're not using the -quiet parameter.

So your command is actually pretty decent. By using ForEach, you're keeping the piped-in computer name. It "knows" if it's up or down by pinging it - literally sending an ICMP packet. Of course, if ICMP is blocked (meaning ping won't work) by a firewall, it'll "think" it's down. Test-Connection -quiet doesn't return a status code or anything - it's just a True/False. True if the machine responded to a ping, False if it didn't. 

Try running Test-Connection -comp SERVER -quiet, all by itself, with computer names from your network. You'll see that it just spits out a True/False.

So, in your command, that True/False is the result from a parenthetical command, right? So the If construct just looks to see if that result is True or False. If it's True, it executes the first script block (in which you're outputting $null). Otherwise, it goes to the Else block.

I'd actually rewrite that:

Get-Content machines.txt | ForEach { if (-not (test-connection -comp $_ -quiet)) { Write-Host "$_ is down" -fore red }}

There's no point in having a $null in there; just test for the condition you want. 

Top 200 Contributor
Posts 8

thanks .....you answered the question:

So, in your command, that True/False is the result from a parenthetical command, right? So the If construct just looks to see if that result is True or False. If it's True, it executes the first script block (in which you're outputting $null). Otherwise, it goes to the Else block.

I'll try to test just for the condition i want next time....:-)

Not Ranked
Posts 1

Hi Don,

I tried your solution and recevied one error. I assume it is last pass with no computer name:

Get-Content names.txt | Foreach {if(-not(Test-Connection -ComputerName $_ -Quiet)){Write-Host " $_ is down" -ForegroundColor RED}}

 cwdp-nine is down

 


Test-Connection : Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.
At line:1 char:71
+ Get-Content names.txt | Foreach {if(-not(Test-Connection -ComputerName <<<<  $_ -Quiet)){Write-Host " $_ is down" -ForegroundColor RED}}
    + CategoryInfo          : InvalidData: (:) [Test-Connection], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.TestConnectionCommand

Any suggestions?

Chris Kirk

 

 

Top 10 Contributor
Posts 640

Your text file probably has a blank line at the end. Test-Connection will fail if you pipe in a blank name.

Top 100 Contributor
Posts 16

Hi Don,

I would like to create an output file for this:

get-content c:\Temp\Pingable.txt | foreach { if (test-connection $_ -quiet) { write-host "$_ is Pingable" } else { write-host "$_ is down"}}

using out-file or export-csv, could you please suggest? I tried to do that with pipe and variable, but could not find the right place, should I put foreach part in a variable?

Top 25 Contributor
Posts 90

write-host only writes to the console. try this:

get-content c:\Temp\Pingable.txt | `
foreach {
if (test-connection $_ -quiet) { write-output "$_ is Pingable" | out-file C:\results.txt -append }
else { write-output "$_ is down" | out-file C:\results.txt -append}
}
Top 100 Contributor
Posts 16

Thank you, pyro3113, that worked well:

get-content c:\Temp\Pingable.txt | foreach { if (test-connection $_ -quiet) { write-output "$_ is Pingable" | out-file C:\results.txt -append } else { write-output "$_ is down" | out-file C:\results.txt -append}}

Thanks,

Roland 

Page 1 of 1 (8 items) | RSS
Copyright 2012 PowerShell.com. All rights reserved.