Wednesday, March 30, 2011

powershell exchange : if then statement syntax?

In my script i'm trying to test for true and false. Is this syntax incorrect?

$pdaout = ""
if ($pda.ActiveSyncEnabled.tostring() -like "True") {$pdaout = "TRUE"}
if ($pda.ActiveSyncEnabled.tostring() -like "False") {$pdaout = "-"}

write-host $pdaout
From stackoverflow
  • It should be, except may be for the tostring() function (use toString() to make sure there is no issue with the case sensitivity)

    Plus, you may want to use elseif to avoid doing a second test if the first one was successful:

    if ($pda.ActiveSyncEnabled.toString() -like "True") {$pdaout = "TRUE"}
    elseif ($pda.ActiveSyncEnabled.toString() -like "False") {$pdaout = "-"}
    
  • Seems like it would be better to just check the boolean value directly instead of using ToString():

    $pdaout = ""
    
    if ($pda.ActiveSyncEnabled -eq $True) { $pdaout = "TRUE" }
    else { $pdaout = "-" }
    
    write-host $pdaout
    

    Here is a blog post from the Windows Powershell team re: Boolean Values and Operators

0 comments:

Post a Comment