Powershell don';在按下某个键之前不要继续
我有一个脚本,它正在等待用户输入,然后根据他们按下的按钮来执行某些操作。但是,问题是不管他们按什么键,脚本都会继续(但是如果它不是预期的两个键之一,我只会得到错误)。这是我正在制作的剪报Powershell don';在按下某个键之前不要继续,powershell,Powershell,我有一个脚本,它正在等待用户输入,然后根据他们按下的按钮来执行某些操作。但是,问题是不管他们按什么键,脚本都会继续(但是如果它不是预期的两个键之一,我只会得到错误)。这是我正在制作的剪报 Write-Host "What do you want to do next?" -nonewline Write-Host " u - Search for another user c - Enter a computer name " # Prompt for an action
Write-Host "What do you want to do next?" -nonewline
Write-Host "
u - Search for another user
c - Enter a computer name
"
# Prompt for an action
Write-Host ">> Select shortcut action: " -nonewline
$key = [Console]::ReadKey()
$value = $key.KeyChar
switch($value) {
c { $c = Read-Host "Enter computer or IP"}
u { $u = Read-Host "Enter user" }
}
# now we continue on with the code depending on what was pressed
我想要的是,如果按下
c
或u
以外的任何键,告诉用户这不是一个有效的键,然后返回此剪报的顶部,再次提示用户下一步要做什么。只需将代码包装在do while
循环中,只要$value
不是c
或u
:
do
{
$key = [Console]::ReadKey($true)
$value = $key.KeyChar
switch($value) {
c { $c = Read-Host "Enter computer or IP"}
u { $u = Read-Host "Enter user" }
}
}
while ($value -notmatch 'c|u')