Scriptblock exceptions are not returned
See original GitHub issueThe following yields no output:
Start-ThreadJob {throw "foo"} | Receive-Job -Wait
If you use Start-Job instead the exception is returned as a non-terminating error. Is there any way Start-ThreadJob could do the same?
Issue Analytics
- State:
- Created 6 years ago
- Comments:14 (9 by maintainers)
Top Results From Across the Web
Scriptblock exceptions are not returned in object-events
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 ......
Read more >About PowerShell Scriptblock non work problem
Hi All, I created PowerShell went to auto download adobe reader and auto install, but always show exitcode:87 My Script: ...
Read more >Error message not helpful when missing scriptblock #498
The message that I got about an ampersand looks like some code fails to handle a very predictable exception. Can we please fix...
Read more >Invoke-Command ScriptBlock issue
In summary I want to know how I can use variables in script block and such that command can be ... Exception] {...
Read more >Powershell Script returning status code
When running an action PowerShell run script block it is always passing and not returning the status code in case of exception.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@ajansveld Thanks for pointing this out! The reason this terminating error isn’t reported is because, unfortunately, PowerShell doesn’t currently provide a public or protected accessible way to report it as part of the job state change. I was aware of this when writing the code but neglected to include a TODO comment and mention this in the release notes. https://github.com/PaulHigin/PSThreadJob/blob/master/ThreadJob/ThreadJob/PSThreadJob.cs#L207
The only way I can see to fix this is to modify PowerShell API to provide protected class access to setting job state along with an optional exception (which would be the terminating exception). This is currently provided as internal access but derived classes such as ThreadJob cannot access the API.
I am sure we can get this change into PSCore (https://github.com/powershell/powershell) but then we would have compatibility issues with Windows PowerShell. But I guess we can have different versions of ThreadJob targeted to different versions of PowerShell.
There is another problem similar to this that I did mention in the release notes. And that is support for processing a $using: variable is also internal access to PowerShell, and so ThreadJob cannot support the using variable without first modifying PowerShell. I’ll create an issue for this as well.
I was able to fix this by using Job2 and implementing JobSourceAdapter.