在循环中按热键重新运行脚本,但不要等待它
Press hotkey to rerun script during a loop but don't wait for it
基本上我有一个运行的脚本,它开始睡眠 30 分钟,然后再次循环并重新运行。我想知道是否有一种方法可以让您点击 window,按下热键,然后让它手动刷新?
while($true){
$data = @("device1", "device2")
ForEach ($server in $data) {
ping $server
start-sleep 1800
clear
}
}
我猜,你最好的选择是在你的主 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/
How to stop while($true) that contains start-sleep in powershell
Is there a way to catch ctrl-c and ask the user to confirm?
我通过其他方法发现,下面的方法允许我检查屏幕是否有任何按键按下,如果有则中断,但仍然处于 start-sleep 状态。
$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
}
基本上我有一个运行的脚本,它开始睡眠 30 分钟,然后再次循环并重新运行。我想知道是否有一种方法可以让您点击 window,按下热键,然后让它手动刷新?
while($true){
$data = @("device1", "device2")
ForEach ($server in $data) {
ping $server
start-sleep 1800
clear
}
}
我猜,你最好的选择是在你的主 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/
How to stop while($true) that contains start-sleep in powershell
Is there a way to catch ctrl-c and ask the user to confirm?
我通过其他方法发现,下面的方法允许我检查屏幕是否有任何按键按下,如果有则中断,但仍然处于 start-sleep 状态。
$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
}