【发布时间】:2021-09-12 17:40:50
【问题描述】:
我正在尝试提示用户输入,然后验证用户输入是否匹配两个不同选项之一,如果没有给出正确的输入,我将退出。
在示例中,我要求用户输入“BOB”或“TOM”作为有效输入,但是当我运行此代码时,我总是会收到消息“未正确输入服务器类型”,即使我输入 BOB 作为提示的输入。
$ServerType = Read-Host -Prompt 'Enter Server Type (BOB or TOM)'
If ($ServerType -ne "BOB" -Or $ServerType -ne "TOM")
{
Write-Host -NoNewLine 'Server type not entered correctly'
}
我也试过
If (($ServerType -ne "BOB") -or ($ServerType -ne "TOM"))
{
Write-Host -NoNewLine 'Server type not entered correctly'
}
但是,当我只测试一个值时,它会起作用:
If ($ServerType -ne "BOB")
{
Write-Host -NoNewLine 'Server type not entered correctly'
}
任何想法为什么我可能会得到这个?
【问题讨论】:
-
在这种情况下您应该使用的运算符应该是
-and。$true OR $false = $true这就是为什么你总是进入那个条件。
标签: windows powershell logical-operators