Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell Job Progress Monitoring

Is there a way for a PowerShell job to report progress or to trigger events before it is complete? I just started playing around with executing background jobs in PowerShell and am wondering how far I can push the capability.

like image 904
Eric Schoonover Avatar asked Dec 18 '22 06:12

Eric Schoonover


1 Answers

The standard "manual" way is to use the Get-Job cmdlet. As for events, when you create a job with Start-Job, the Job object returned has a "StateChanged" event on it you can subscribe to e.g.:

$job = Start-Job { Get-Process; Start-Sleep -seconds 60 }
Register-ObjectEvent $job -EventName StateChanged `
    -SourceIdentifier JobStateChanged `
    -Action { Write-Host "Job $($Sender.Id) $($Sender.JobStateInfo)" }
like image 119
Keith Hill Avatar answered Dec 26 '22 01:12

Keith Hill