3

我想在 PowerShell 中做一个简单的计数器。它必须提示用户他们想要更高或更低的数字。

起始数字必须为0,不能小于0或大于10。如果用户想要更大的数字,则必须将数字增加1,如果较低的数字减少1。那么它必须能够停止在所需的数字。有了这个数字,我可以设置一个注册表值。

我不知道提示用户的有效方法。我可以使用Read-Host cmdlet 来询问他们输入的是“更高”还是“更低”,但有没有更有效的方法来完成此操作?

例如,

$i = 0

while (($i -gt 0) -or ($i -lt 10)){
    $j = Read-Host "The current number is $i, would you like a higher/lower number, or quit?"

    if ($j -eq "higher") {
        $i++
        Write-Host "The current number is $i"
    } elseif ($j -eq "lower") {
        $i--
        Write-Host "The current number is $i"
    } elseif ($j -eq "quit") {
        Write-Host "Final number is: $i"
        break
    }
}

我怎样才能做到这一点?

4

1 回答 1

3

您可以使用是/否提示窗口来获取用户输入。

$a = new-object -comobject wscript.shell
$intAnswer = $a.popup("Do you want to delete these files?", 0, "Delete Files", 4)
If ($intAnswer -eq 6) {
    $a.popup("You answered yes.")
}
else {
    $a.popup("You answered no.")
}

如果您替换 popup() 函数的第四个参数中的“3”,您将在提示窗口中获得是、否和取消按钮。

参考:在 Windows PowerShell 中提供是/否提示

于 2013-04-05T11:02:09.917 回答