h.s*_*h.s 5 powershell powershell-3.0
就像我说的,这段代码适用于 PowerShell 版本 2,但不适用于 PowerShell 版本 5。
function wait
{
$compte = 0
Write-Host "To continue installation and ignore configuration warnings type [y], type any key to abort"
While(-not $Host.UI.RawUI.KeyAvailable -and ($compte -le 20))
{
$compte++
Start-Sleep -s 1
}
if ($compte -ge 20)
{
Write-Host "Installation aborted..."
break
}
else
{
$key = $host.ui.rawui.readkey("NoEcho,IncludeKeyup")
}
if ($key.character -eq "y")
{Write-Host "Ignoring configuration warnings..."}
else
{Write-Host "Installation aborted..."
}}
Run Code Online (Sandbox Code Playgroud)
官方文档或会告诉您Read-Host -?不可能Read-Host以这种方式使用。没有可能的参数告诉它以某种超时运行。
但还有其他各种问题详细说明了如何在 PowerShell(通常使用 C#)中执行此操作。
这个想法似乎是检查用户何时按下某个键$Host.UI.RawUI.KeyAvailable并检查超时持续时间。
一个简单的工作示例如下:
$secondsRunning = 0;
Write-Output "Press any key to abort the following wait time."
while( (-not $Host.UI.RawUI.KeyAvailable) -and ($secondsRunning -lt 5) ){
Write-Host ("Waiting for: " + (5-$secondsRunning))
Start-Sleep -Seconds 1
$secondsRunning++
}
Run Code Online (Sandbox Code Playgroud)
您可以用来$host.UI.RawUI.ReadKey获取按下的键。如果您需要比简单按下按钮更复杂的输入,则此解决方案可能不可接受。也可以看看:
| 归档时间: |
|
| 查看次数: |
4549 次 |
| 最近记录: |