views:

402

answers:

2

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.

+5  A: 

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)" }
Keith Hill
+2  A: 

You can also raise your own custom events from within a local or remote job and act on them in the local session. I'm writing a brief blog entry on this; watch this space.

-Oisin

powershell mvp

www.nivot.org

x0n
update this answer with a proper link when it's posted
spoon16