如何否定 PowerShell 中的条件? [英] How do I negate a condition in PowerShell?
本文介绍了如何否定 PowerShell 中的条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在 PowerShell 中否定条件测试?
How do I negate a conditional test in PowerShell?
例如,如果我想检查目录C:\Code,我可以运行:
For example, if I want to check for the directory C:\Code, I can run:
if (Test-Path C:\Code){
write "it exists!"
}
有没有办法否定这种情况,例如(非工作):
Is there a way to negate that condition, e.g. (non-working):
if (Not (Test-Path C:\Code)){
write "it doesn't exist!"
}
<小时>
解决方法:
if (Test-Path C:\Code){
}
else {
write "it doesn't exist"
}
这很好用,但我更喜欢内联.
This works fine, but I'd prefer something inline.
推荐答案
Not
几乎让您满意.应该是:
You almost had it with Not
. It should be:
if (-Not (Test-Path C:\Code)) {
write "it doesn't exist!"
}
您也可以使用 !
: if (!(Test-Path C:\Code)){}
为了好玩,您也可以使用按位互斥,或者,尽管它不是最易读/易理解的方法.
Just for fun, you could also use bitwise exclusive or, though it's not the most readable/understandable method.
if ((test-path C:\code) -bxor 1) {write "it doesn't exist!"}
这篇关于如何否定 PowerShell 中的条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文