[英]Press hotkey to rerun script during a loop but don't wait for it
Basically I have a script that runs and it Start-Sleep for 30 minutes then loops around again and re-runs. 基本上,我有一个可以运行的脚本,它开始休眠30分钟,然后再次循环并重新运行。 I was wondering if there was a way where you could click on the window, press a hotkey, and have it just refresh manually?
我想知道是否有一种方法可以单击窗口,按热键,然后手动刷新?
while($true){
$data = @("device1", "device2")
ForEach ($server in $data) {
ping $server
start-sleep 1800
clear
}
}
I found through other methods that the below has allowed me to check the screen for any key press and break if there is one, but still be in a start-sleep status. 通过其他方法,我发现以下内容使我可以检查屏幕上是否有任何按键,如果有按键,请中断该按键,但仍处于开始睡眠状态。
$timeout = New-TimeSpan -Minutes 30
$sw = [diagnostics.stopwatch]::StartNew()
while ($sw.elapsed -lt $timeout){
if ($host.UI.RawUI.KeyAvailable) {
$key = $host.UI.RawUI.ReadKey()
break
}
start-sleep -seconds 5
}
My guess, your best bet is to implement a for
loop inside your main while loop that does check for key press once every 5-15 seconds and if it detects that, it breaks itself, so your while loop can start over, here are a few links that seem to be relevant: 我的猜测是,您最好的选择是在主while循环中实现一个
for
循环,该循环每5-15秒检查一次按键,如果检测到它,它会自行中断,因此while循环可以重新开始,这是一个几个似乎相关的链接:
Waiting for user input with a timeout 等待用户输入超时
https://blogs.msdn.microsoft.com/timid/2014/01/29/read-host-with-a-timeout-kind-of/ https://blogs.msdn.microsoft.com/timid/2014/01/29/read-host-with-a-timeout-kind-of/
How to stop while($true) that contains start-sleep in powershell 如何在PowerShell中停止包含start-sleep的while($ true)
Is there a way to catch ctrl-c and ask the user to confirm? 有没有办法捕获ctrl-c并要求用户确认?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.