Receive-Job
yields no output on Start-ThreadJob
- or Start-Job
-jobs, when listening to Completed
or Failed
JobState changes with Register-ObjectEvent
.
$PSVersionTable dump:
Name Value
---- -----
PSVersion 5.1.17763.2183
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.17763.2183
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
Steps to reproduce the issue:
$job = Start-ThreadJob -ScriptBlock { throw "Error trying to do a task" }
Register-ObjectEvent $job StateChanged -Action {
Write-Host ('Job #{0} ({1}) changed state: {2}' -f $sender.Id, $sender.Name, $sender.State)
if (($sender.State -eq 'Completed') -or ($sender.State -eq 'Failed')) {
Receive-Job $sender
}
}
When waiting for the job with Wait-Job
(like suggested in this answer) the exception returns correctly, but I need a non-blocking approach. If you use Start-Job
instead of Start-ThreadJob
, the exception doesn't return in the event-based approach, too.
Any ideas on what I'm doing wrong?