Technet关于 Windows PowerShell 4.0 的 about_Logical_Operators 声明如下:
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
?
答案 0 :(得分:14)
一组简单的测试用例表明短路有效:
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
答案 1 :(得分:0)
我一直在寻找相同的答案。我认为到目前为止最好的...
$cueNumber = 512
@{ $true = "This is true"; $false = "This is false" }[$cueNumber -gt 500]
参考:https://adamtheautomator.com/a-simpler-ifthen-conditional-logic-in-powershell/