Technet的[about_Logical_Operators关于Windows PowerShell 4.0指出以下内容:
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.
似乎都没有执行短路评估。
如何模仿Windows Powershell 4.0中的C# ||
或VB OrElse
?
一组简单的测试用例表明短路有效:
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
我一直在寻找相同的答案。我认为到目前为止最好的...
$cueNumber = 512
@{ $true = "This is true"; $false = "This is false" }[$cueNumber -gt 500]
参考:https://adamtheautomator.com/a-simpler-ifthen-conditional-logic-in-powershell/