views:

595

answers:

2

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
A: 

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 = "-"}
VonC
+3  A: 

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

spoon16