我有一个启动作业的 powershell 脚本
start-job -scriptblock {
while($true) {
echo "Running"
Start-Sleep 2
}
}
然后它继续执行脚本的其余部分。
该工作是一种对该进程 PID 的监控工作。
我想每n秒同步打印一次PID,而不必结束作业。
例如,当脚本的其余部分正在执行时,我希望在控制台中看到输出。
powershell 中可能有类似的事情吗?
Thanks.
是的,您可以使用事件:
$job = Start-Job -ScriptBlock {
while($true) {
Register-EngineEvent -SourceIdentifier MyNewMessage -Forward
Start-Sleep -Seconds 3
$null = New-Event -SourceIdentifier MyNewMessage -MessageData "Pingback from job."
}
}
$event = Register-EngineEvent -SourceIdentifier MyNewMessage -Action {
Write-Host $event.MessageData;
}
for($i=0; $i -lt 10; $i++) {
Start-Sleep -Seconds 1
Write-Host "Pingback from main."
}
$job,$event| Stop-Job -PassThru| Remove-Job #stop the job and event listener
信用去往这个答案 https://stackoverflow.com/a/8418338/897326。其他有用的链接:
- 如何让 Windows PowerShell 在作业完成时通知您 http://answers.oreilly.com/topic/1980-how-to-get-windows-powershell-to-notify-you-when-a-job-is-complete/
- 使用 PowerShell 管理事件订阅 - 嘿,脚本专家!博客 http://blogs.technet.com/b/heyscriptingguy/archive/2011/06/17/manage-event-subscriptions-with-powershell.aspx
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)