Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scriptblock exceptions are not returned in object-events #66

Open
Marvin-Keller opened this issue Dec 2, 2021 · 0 comments
Open

Scriptblock exceptions are not returned in object-events #66

Marvin-Keller opened this issue Dec 2, 2021 · 0 comments

Comments

@Marvin-Keller
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant