Use the Register-TemporaryEvent command to register for the event’s StateChanged event:
PS > $job = Start-Job -Name TenSecondSleep { Start-Sleep 10 }
PS > Register-TemporaryEvent $job StateChanged -Action {
[Console]::Beep(100,100)
Write-Host "Job #$($sender.Id) ($($sender.Name)) complete."
}
PS > Job #6 (TenSecondSleep) complete.
PS >When a job completes, it raises a StateChanged event to notify subscribers that its state has changed. We can use PowerShell’s event handling cmdlets to register for notifications about this event, but they are not geared toward this type of one-time event handling. To solve that, we use the Register-TemporaryEvent command.
In our example action block in that solution, we simply emit a beep and write a message saying that the job is complete.
As another option, you can also update your prompt function to highlight jobs that are complete but still have output you haven’t processed:
$psJobs = @(Get-Job -State Completed | ? { $_.HasMoreData })
if($psJobs.Count -gt 0) { ($psJobs | Out-String).Trim() | Write-Host -Fore Yellow }This introduction to the Windows PowerShell language and scripting environment provides more than 430 task-oriented recipes to help you solve the most complex and pressing problems, and includes more than 100 tried-and-tested scripts that intermediate to advanced system administrators can copy and use immediately. You'll find hands-on tutorials on fundamentals, common tasks, and administrative jobs that you can apply whether you're on a client or server version of Windows.




Help






