How do I perform short circuit evaluation in Windows PowerShell 4.0?

The Technet about_Logical_Operators regarding Windows PowerShell 4.0 states the following:

Operator     Description                        Example

--------     ------------------------------     ------------------------
-or          Logical or. TRUE when either       (1 -eq 1) -or (1 -eq 2) 
             or both statements are TRUE.       True

-xor         Logical exclusive or. TRUE         (1 -eq 1) -xor (2 -eq 2)
             only when one of the statements    False 
             is TRUE and the other is FALSE.

      

None of them perform short circuit assessment.

How can I simulate C # ||

or VB OrElse

in Windows Powershell 4.0 ?

+3


source to share


1 answer


A simple set of test cases shows that short circuit works:



PS C:\> 1 -eq 0 -or $(Write-Host 'foo')
foo
False
PS C:\> 1 -eq 1 -or $(Write-Host 'foo')
True

PS C:\> 1 -eq 1 -and $(Write-Host 'foo')
foo
False
PS C:\> 1 -eq 0 -and $(Write-Host 'foo')
False

      

+8


source







All Articles