phill
phill

Reputation: 13864

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

Upvotes: 0

Views: 6826

Answers (2)

Eric Schoonover
Eric Schoonover

Reputation: 48412

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

Upvotes: 3

VonC
VonC

Reputation: 1328572

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 = "-"}

Upvotes: 0

Related Questions