Skip to content

Scriptblock exceptions are not returned in object-events #66

Description

@marvin-keller

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions