Powershell Start-job synchronous output Powershell Start-job synchronous output powershell powershell

Powershell Start-job synchronous output


Yes, you can use events:

$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

Credit goes to this answer. Other useful links: