nik*_*tsa 7 powershell scripting start-job
我有一个启动工作的powershell脚本
start-job -scriptblock {
while($true) {
echo "Running"
Start-Sleep 2
}
}
Run Code Online (Sandbox Code Playgroud)
然后它继续执行脚本的其余部分.
那项工作对于该过程的PID来说是一种监控.
我想每隔n秒同步打印PID,而不必结束工作.
例如,当正在执行脚本的其余部分时,我希望在我的控制台中看到输出.
在PowerShell中有类似的东西吗?
谢谢.
Neo*_*isk 13
是的,你可以使用事件:
$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
Run Code Online (Sandbox Code Playgroud)
归功于这个答案.其他有用的链接:
归档时间: |
|
查看次数: |
2987 次 |
最近记录: |