我有一个启动工作的powershell脚本
start-job -scriptblock { while($true) { echo "Running" Start-Sleep 2 } }
然后它继续执行脚本的其余部分.
那项工作对于该过程的PID来说是一种监控.
我想每隔n秒同步打印PID,而不必结束工作.
例如,当正在执行脚本的其余部分时,我希望在我的控制台中看到输出.
在PowerShell中有类似的东西吗?
谢谢.
是的,你可以使用事件:
$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
归功于这个答案.其他有用的链接:
如何让Windows PowerShell在作业完成时通知您
使用PowerShell管理事件订阅 - 嘿,脚本专家!博客