Posts Tagged ‘Caveat’

Powershell pitfalls

Jun 21st, 2018
1
2
3
function T ($b){Write-Host "[$b]"; return $b}

if( T($true) -and T($true)){1}else{0}

I would expect the above to return

1
2
3
[True]
[True]
1

but no. Instead

1
2
[True]
1

is returned.

Why?

It’s because a method call in an if statement has to be surrounded with paranthesises like so:

1
if( ( T($true)) -and (T($true)) ){1}else{0}

Now the output is

1
2
3
[True]
[True]
1