Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell don';在按下某个键之前不要继续_Powershell - Fatal编程技术网

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')