powershell将字符串与-或进行比较
我试图验证变量的值,但不管怎样,如果不使用-或,我只能得到正确的结果powershell将字符串与-或进行比较,powershell,Powershell,我试图验证变量的值,但不管怎样,如果不使用-或,我只能得到正确的结果 if (!$SER -eq "Y" -or !$SER -eq "N"){ write-host "ERROR: Wrong value for services restart" -foreground "red" } 还是像这样 if (-not($SER -eq "Y") -or -not($SER -eq "N")){ write-host "ERROR: Wrong value for serv
if (!$SER -eq "Y" -or !$SER -eq "N"){
write-host "ERROR: Wrong value for services restart" -foreground "red"
}
还是像这样
if (-not($SER -eq "Y") -or -not($SER -eq "N")){
write-host "ERROR: Wrong value for services restart" -foreground "red"
}
这起作用(ne
表示不相等):
这也适用于:
if ("Y", "N" -notcontains $SER) {
Write-Host "ERROR: Wrong value for services restart" -ForegroundColor Red
}
自从PowerShell v3以来:
if ($SER -notin "Y", "N") {
Write-Host "ERROR: Wrong value for services restart" -ForegroundColor Red
}
。但是,您可以使用-notin
:
if ($SER -notin 'Y', 'N') {
Write-Host "ERROR: Wrong value for services restart" -ForegroundColor Red
}
if ($SER -notin 'Y', 'N') {
Write-Host "ERROR: Wrong value for services restart" -ForegroundColor Red
}