Search code examples
powershellpingstatus

PowerShell Test-Connection to multiple IPs and do xyz on Results


I'm currently working on a line of code to do a ping test to multiple IPs and have it return a single status update. Im trying to have it so if all results are $true then show Green, if results contain a $false but there is also a $true then show Yellow and finally if all results are $false then show Red.

Below is what I've got. This is one iteration of stuff I've tried but I can only get it to respond with Yellow and not Red. Any assistance is greatly appreciated.

$octets = (Get-NetIPAddress | ?{$_.PrefixOrigin -eq "Manual","Dhcp"}).IPAddress.Split(".")
$ipFirst3 = $octets[0] + "." + $octets[1] + "." + $octets[2]
$ESX1MGT = $ipFirst3 + '.' + "181" #esxi management
$ESX1STG = $ipFirst3 + '.' + "151" #esxi storage
$ESX1VMO = $ipFirst3 + '.' + "71"  #esxi vmotion

If(Test-Connection $ESX1MGT -count 2 -Quiet){
        $MGTStatus = "True"
    }else{
        $MGTStatus = "False"
        }
    If(Test-Connection $ESX1STG -count 2 -Quiet){
        $STGStatus = "True"
    }else{
        $STGStatus = "False"
        }
    If(Test-Connection $ESX1VMO -count 2 -Quiet){
        $VMOStatus = "True"
    }else{
        $VMOStatus = "False"
        }

    $ESX1Status = @($MGTStatus,$STGStatus,$VMOStatus)
    If($ESX1Status -eq "True"){
        $ESX1StatusLabel.BackColor = "Green"
    }elseif ($ESX1Status -contains "False") {
        $ESX1StatusLabel.BackColor = "Yellow"
    }elseif ($ESX1Status -eq "False") {
        $ESX1StatusLabel.BackColor = "Red"
    }

EDIT Here is another Ideration I have tried

$ESX1Status = @($ESX1MGT,$ESX1STG,$ESX1VMO)
$result = Foreach($ESX in $ESX1Status){
    Test-Connection $ESX -Count 2 -Quiet
    }
If($result -is $true){
    $ESX1StatusLabel.BackColor = "Green"
}
If($result -contains $false -and $true){
    $ESX1StatusLabel.BackColor = "Yellow"
}
If($result -is $false){
    $ESX1StatusLabel.BackColor = "Red"
}

When running both versions I sometimes get this error

Cannot convert value "True" to type "System.Type". Error: "Invalid cast from 'System.Boolean' to 'System.Type'."
At C:\Source\Scripts\PowerShell\Verification.ps1:420 char:4
+ If($result -is $true){
+    ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], RuntimeException
    + FullyQualifiedErrorId : RuntimeException

Solution

  • This ended up working out perfect for me. Answer was from a post on Reddit I made as well in r/PowerShell from u/engageant

    “There's an easy way to simplify all of this:

    $mgmt = Test-Connection $ESX1MGT -Count 2 -Quiet
    $storage = Test-Connection $ESX1STG -Count 2 -Quiet
    $vmotion = Test-Connection $ESX1VMO -Count 2 -Quiet
    $status = $mgmt + $storage + $vmotion
    switch ($status) {
      3 {ESX1StatusLabel.BackColor = "Green"}
      2 {ESX1StatusLabel.BackColor = "Yellow"}
      1 {ESX1StatusLabel.BackColor = "Yellow"}
      0 {ESX1StatusLabel.BackColor = "Red"}
    }
    
    

    What's happening here is we're getting the result of each test into its own variable, then summing the variables (remember 0 = false, 1 = true). We then evaluate the sum and act accordingly. There are other tweaks that can be made here to possibly increase readability/scalability, but I've intentionally been explicit for the purposes of explaining the "how".”