1

I have coded this small script. Everything is working fine, the if-statement excluded. It doesn't matter which value the variable $status results. The if-statement results always in the else condition.

Is it quite possible to work with the properties of a Cmdlet result in an if-statement?

$filepath = "C:\Scripts\Logs"
$date = Get-Date -Format "dd.MM.yyyy HH:mm:ss"

$tapejob = Get-VBRTapeJob -Name "GFS Backup to Tape"
$tapejob | Disable-VBRJob

$status = Get-VBRTapeJob -Name "GFS Backup to Tape"

if ($status.Enabled -eq "False") 
{"$date Der Backup To Tape Job wurde erfolgreich deaktiviert." | Out-File "$filepath\DisableTapeJobLog.txt" -Append}
elseif ($status.Enabled -eq "True") 
{"$date Der Backup To Tape Job wurde nicht erfolgreich deaktiviert." | Out-File "$filepath\DisableTapeJobLog.txt" -Append}
else 
{"$date Irgendwas stimmt hier nicht. :-)" | Out-File "$filepath\DisableTapeJobLog.txt" -Append}
2
  • What exactly are the values of $status.Enabled? If you run it through get-member, what is the type of Enabled (bool, string or something else)? Going to the else branch seems to suggest that Enabled might be $null, or maybe the property doesn't even exist. Commented Nov 9, 2022 at 11:28
  • The property Enabled provides a boolean value. If I query $status.Enabled I get the correct value. Commented Nov 9, 2022 at 12:58

1 Answer 1

1

Note:

  • In order to detect the case where $status.Enabled is not a Boolean (such as $null, if no .Enabled property exists), use the following as the first if conditional:

    if ($status.Enabled -isnot [bool]) {
      "$date Irgendwas stimmt hier nicht. :-)" | Out-File "$filepath\DisableTapeJobLog.txt" -Append
    }
    
  • The next section assumes that $status.Enabled returns a Boolean (bool); the complete if statement is in the bottom section.


Use ($status.Enabled -eq $false) or, preferably and more succinctly,
(-not $status.Enabled)

Generally speaking:

  • There is no need to compare a Boolean value against another value explicitly (see next point), but if you do, and that other value isn't already a Boolean, you need to understand how PowerShell converts it to a Boolean - see next section.

  • It's best to use Boolean values implicitly in conditionals; that is:

    • use $someBooleanValue in lieu of $someBooleanValue -eq $true
    • use -not $someBooleanValue in lieu of $someBooleanValue -eq $false
  • As an aside: If you already know PowerShell's to-Boolean conversion rules, you can use any value implicitly as a Boolean, simply by using it as-is in a conditional and relying on PowerShell to convert it to a Boolean; e.g.,
    $val = 'nonempty string'; if ($val) { 'yes' } outputs yes, because any nonempty string is coerced to $true in a Boolean context (see below).


As for what you tried:

($status.Enabled -eq "False")

Comparing a Boolean value ($true or $false) to a string value such as "False" doesn't work as you might expect:

Due to the LHS being a Boolean, PowerShell coerces the RHS to a Boolean too before comparing, and PowerShell's to-Boolean conversion rules consider any nonempty string to be $true, irrespective of its value.

That is, [bool] "False" yields $true(!), because "False" is a nonempty string.

Therefore, your comparison is equivalent to $status.Enabled -eq [bool] "False", which is the same as $status.Enabled -eq $true(!), i.e. the opposite of your intent.


See the bottom section of this answer for a summary of PowerShell's to-Boolean conversion rules.


To put it all together:

if ($status.Enabled -isnot [bool]) {
  "$date Irgendwas stimmt hier nicht. :-)" | Out-File "$filepath\DisableTapeJobLog.txt" -Append
}
elseif (-not $status.Enabled) {
  "$date Der Backup To Tape Job wurde erfolgreich deaktiviert." | Out-File "$filepath\DisableTapeJobLog.txt" -Append
}
else {
  "$date Der Backup To Tape Job wurde nicht erfolgreich deaktiviert." | Out-File "$filepath\DisableTapeJobLog.txt" -Append
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.