-2

If using Test-Connection on multiple computers with -Quiet how do I know which result is for which computer?

e.g.

$computers = ("PC1","PC2","PC3")

$results = Test-Connection -ComputerName $computers -count 2 -quiet

EDITED TO SHOW POINT I AM MAKING IN DISCUSSION BELOW

Using without -quiet

$computers = ("PC1","PC2","PC3")

$results = Test-Connection -ComputerName $computers -count 2 

PC3 not responding so get error

Test-Connection : Testing connection to computer 'PC3' failed: No such host is known At line:3 char:16

And $results only contains those 2 computers which worked

How to get $results to contains results for all 3 computers - with status of failure for PC3

Darren Rose
  • 169
  • 13
  • If you don't want just a `True` or `False` result. You will need to either drop the `-Quiet` switch or loop through your array of `$computers` so you can include logic on each computer string being processed. – AdminOfThings Feb 01 '21 at 18:46
  • Okay, Thanks - I had hoped there would be a way to get the computer name without resorting to that. Trouble is with -Quiet switch it works well when getting error such as "no such host is known" as just returns false, but without -Quiet switch I get error and no results for that host – Darren Rose Feb 01 '21 at 18:56
  • This is basicallly the same question i have answered [here](https://stackoverflow.com/a/65993716/9898643), only there the PowerShell code is wrapped in VB.Net – Theo Feb 01 '21 at 19:46
  • @Theo - yes and no, going back to basics of getting what I want working in Powershell before wrapping it in VB, and question is different as per edit above re not using -quiet switch – Darren Rose Feb 01 '21 at 19:48

2 Answers2

1

-Quiet outputs a Boolean type. That can only be True or False. So you cannot use -Quiet without extra code to get the result you want.


It appears the results of Test-Connection are in corresponding order with the -ComputerName bound array values. You can loop through each array with the same index to match computer name to its result.

$computers = 'PC1','PC2','PC3'

$results = Test-Connection -ComputerName $computers -count 2 -quiet

# Creating an array of objects with Computer and Result properties
$output = 0..($computers.Count-1) | Foreach-Object {
    [pscustomobject]@{
        Computer = $computers[$_]
        Result = $results[$_]
    }
}
# Outputs all objects
$output
# Outputs a specific computer result
($output | Where computer -eq 'PC2').Result
AdminOfThings
  • 23,946
  • 4
  • 17
  • 27
  • okay thanks, that is handy to know, but still not as nice as getting the name returned as part of the results, perhaps will have to do without -quiet switch but as above this then gives me issue if getting error such as "no such host is known" for any computers. With -quiet I get returned False, but without -quiet I just get an error or no result for that computer – Darren Rose Feb 01 '21 at 19:02
  • what is the format you want to see for the output? Any strings can be manipulated. `$output` does display the name and the result – AdminOfThings Feb 01 '21 at 19:03
  • I didn't mean "your" output from sample, I mean from standard output as per my initial post. I will be passing several computers and need to then perform next step depending on whether a pass (success) or fail (error), hence may be easier for me to do without -quiet so I get full results, but as I said that doesn't work if one of the computers doesn't respone e.g. "no such host is known" as just get error in console rather than a "fail" result – Darren Rose Feb 01 '21 at 19:08
  • `-Quiet` outputs a `Boolean` type. That can only be `True` or `False`. So you cannot use `-Quiet` without anything else to get the result you want. – AdminOfThings Feb 01 '21 at 19:11
  • Yes I got that. So I will use it without -Quiet - but then I have issue (as I have now said three times) where if one of the computers in my list doesnt respond e.g. "no such host is known", then instead of a fail result I just get an error in the console – Darren Rose Feb 01 '21 at 19:26
  • edited original post to explain fully my point – Darren Rose Feb 01 '21 at 19:39
1

To store failure status for individual computers in the result, I think the only way is to call Test-Connection with single computer. This way you can check for $null result of each ping.

In order to still run the queries in parallel, you could use ForEach-Object -Parallel as in the following example.

$computers = 'localhost', '0.0.0.0', '127.0.0.1'

# Do one call to Test-Connection per computer, but still in parallel
$results = $computers | ForEach-Object -Parallel {

    # $status will be $null on critical error
    $status = Test-Connection -ComputerName $_ -count 2 -quiet

    # This implicitly adds an object with Computer name and status to the output array $result
    [PSCustomObject]@{
        Computer = $_
        Status   = if( $null -ne $status ) { $status } else { 'Failure' }
    }
}

# Sort the results on the computer index
$results | Sort-Object { $computers.IndexOf( $_.Computer ) }

Output:

Computer   Status    
--------   ------    
localhost    True    
0.0.0.0   Failure    
127.0.0.1    True 

The Sort-Object call is there because the $results array can be in any order. It depends on how fast the computers respond.

Instead of outputting "Failure", you could also output error details. You could get the last error record through $Error[0].

zett42
  • 25,437
  • 3
  • 35
  • 72