powershell - 循环直到用户输入三个必需字母之一

标签 powershell do-while

目前我有这个:

do {
  $MAB = Read-Host "Do you require a Mandatory and/or Advertised deployment? (M)andatory, (A)dvertised, (B)oth"
} while ($MAB -ne "M","A","B")

但它对我不起作用。

我需要它做的是询问用户是否需要广告​​、强制或两者,并根据他们的需要键入 M 表示强制,A 表示广告或 B 表示两者。

目前,如果我输入 3 个字母中的任何一个,它会再次询问,使用我上面的内容。

如果我这样设置:

do {
  $MAB = Read-Host "Do you require a Mandatory and/or Advertised deployment? (M)andatory, (A)dvertised, (B)oth"
} while ($MAB -ne "M")

它工作正常,尽管这对我的 A 或 B 输入没有帮助。

此外,如果您能解释一下我如何抛出错误“您必须输入 A 代表广告,B 代表两者,或 M 代表强制,才能继续。”如果他们输入的不是 M、A 或 B。

最佳答案

我认为这很简洁:

do {
  $MAB = Read-Host "Do you require a Mandatory and/or Advertised deployment? (M)andatory, (A)dvertised, (B)oth"
} until ($("M","A","B").Contains($MAB))

关于powershell - 循环直到用户输入三个必需字母之一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34605896/

相关文章:

c++ - 无法计算输出

c - 带 while 循环的读取命令函数

powershell - PowerShell Get-ChildItem完整路径被截断

c# - 通过代理服务器连接到 Office365

powershell - 如何在Powershell脚本中一个接一个地运行多个脚本

powershell - 使用 powershell 启动/停止 IIS 网站时的权限

sql-server - 在租户中找不到 Active Directory 对象 ''

c - 循环永远持续

java - 执行此功能的更好或更标准的方法是什么?

c++ - 为什么不能在 do while 循环的表达式部分声明变量?