Home > Mobile >  Output switching when re-arranging IF statement in Powershell
Output switching when re-arranging IF statement in Powershell

Time:09-17

I am trying to write a script that tests the connection of computers in an OU and then tells me if they are on or off. This is going to be included in a script that has the end function of testing the connection, and if the computer is turned on, then performing a regedit but if it is turned off just skipping the computer and moving to the next one. However I am having trouble with the first bit of my If statement.

ForEach ($computer in $ADComputers)
{$TestConnection = Test-Connection -ComputerName $computer -Quiet

    If ($TestConnection -eq 'True')
        {Write-Host "$computer is on"}
            Else 
                {Write-Host "$computer is off"}}

This code block returns the following results, which are correct

MS04D4C492C172 is on
MS04D4C492BADB is on
MS04D4C4AAE3F2 is on
MS04D4C492BAE2 is off

However doing it in the reverse will be easier for my syntax with the rest of the script that I wish to create, but if I try the following

ForEach ($computer in $ADComputers)
{$TestConnection = Test-Connection -ComputerName $computer -Quiet

    If ($TestConnection -eq 'False')
        {Write-Host "$computer is off"}
            Else 
                {Write-Host "$computer is on"}}

My results are switched and I get this

MS04D4C492C172 is off
MS04D4C492BADB is off
MS04D4C4AAE3F2 is off
MS04D4C492BAE2 is on

I have tried running test connections manually on two of the computers and I get the following results

PS H:\> Test-Connection -ComputerName ms04d4c492c172 -Quiet
True

PS H:\> Test-Connection -ComputerName MS04D4C492BAE2 -Quiet
False

I don't understand why my output is switching. All I am doing is changing it from an if true to an if false statement? or am I doing something really silly here?

CodePudding user response:

You are comparing to the string 'False'. Test-Connection -Quiet gives you a boolean value. Powershell's booleans are $true and $false. Only when printed out they are displayed as True and False. But still, $false and 'False' are different things.

If you use better variable names ($isOnline instead of $testConnection), and coherent indentation:

foreach ($computer in $ADComputers) {
    $isOnline = Test-Connection -ComputerName $computer -Quiet
    if ($isOnline) {
        Write-Host "$computer is on"
    } else {
        Write-Host "$computer is off"
    }
}

and then compare against an actual boolean value...

foreach ($computer in $ADComputers) {
    $isOnline = Test-Connection -ComputerName $computer -Quiet
    if ($isOnline -eq $false) {
        Write-Host "$computer is off"
    } else {
        Write-Host "$computer is on"
    }
}

BUT: Things like if ($booleanValue -eq $true) or if ($booleanValue -eq $false) are bad style.

The boolean value already is true or false. It's completely useless to compare it again. Therefore it's better written as if ($booleanValue) or if (-not $booleanValue), and it also is a lot more readable that way:

foreach ($computer in $ADComputers) {
    $isOnline = Test-Connection -ComputerName $computer -Quiet
    if (-not $isOnline) {
        Write-Host "$computer is off"
    } else {
        Write-Host "$computer is on"
    }
}
  • Related