Powershell - 等待输入 10 秒 [英] Powershell - Wait input for 10 seconds
本文介绍了Powershell - 等待输入 10 秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$inputYN = Read-Host "(defaults to y in 10 sec) [y/n] ";
如果用户在 10 秒内没有提供任何输入,那么默认 $inputYN
应该转到是"并前进到下一个任务.
If user doesn't provide any input in 10 second then default $inputYN
should go to "Yes" and move forward to next task.
推荐答案
你的问题引起了我的兴趣.我不确定它的效果如何,但我测试了几次并找回了正确的密钥.如果您尝试输入大写字母,则效果不佳.但是,必须有比这更好的方法.您可以随时查看其他论坛,例如 PowerShell.com 或 PowerShell.org 以获得不同的人群.
Your question intrigued me. I'm not sure how well this will work, but I tested it a couple of times and got the correct keys back. Doesn't work too well if you try to enter capital letters. There has to be a better way than this, though. You can always check with other forums like PowerShell.com or PowerShell.org to get a different crowd.
function Get-Answer {
$counter = 0
while($counter++ -lt 100){
if($Host.UI.RawUI.KeyAvailable){
#You could check to see if it was the "Shift Key or another"
$key = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyUp")
break
}
else{
Start-Sleep -Milliseconds 100
$key = $false
}
}
return ($key)
}
Write-Host 'Enter y/n'
$answers = Get-Answer
if($answers -eq $false -or ($answers.Character -eq 'y')){
"Yes is default"
}
elseif($answers.Character -eq 'n'){
"NO!!!!"
}
else{
"Invalid Key $($answers.Character): `"YES`" is being used now."
}
这篇关于Powershell - 等待输入 10 秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文