Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upUbiquitous -OnError {ScriptBlock} parameter #6010
Comments
iSazonov
added
Issue-Enhancement
Area-Language
labels
Jan 24, 2018
SteveL-MSFT
added this to the 6.1.0-Consider milestone
Jan 24, 2018
SteveL-MSFT
modified the milestones:
6.1.0-Consider,
6.2.0-Consider
Apr 26, 2018
SteveL-MSFT
modified the milestones:
6.2.0-Consider,
Future
Jun 21, 2018
SteveL-MSFT
modified the milestones:
Future,
6.3-Consider
Feb 19, 2019
This comment has been minimized.
This comment has been minimized.
|
Really like the idea as there isn't really any good way to handle errors from the error stream. The only way known to me is something like this: Get-Whatever 2>&1 |
ForEach-Object {
if ($_ -is [ErrorRecord]) {
# handle error
} else {
$_
}
}However, the |
This comment has been minimized.
This comment has been minimized.
skycommand
commented
Jun 2, 2019
|
So, how does it work for distinguishing the error type and acting accordingly? Let's say there is a script in which a file has to be deleted. The Remove-Item command might end with an error when:
|
This comment has been minimized.
This comment has been minimized.
|
You would presumably get |
rjmholt
assigned
TylerLeonhardt
Jun 3, 2019
This comment has been minimized.
This comment has been minimized.
|
Just to call it out, PR #8205 is currently in the review process and should be in place in PowerShell 7 shortly (it's just waiting for PR #9825 to finish review and be merged so that automated scripts can be written against the debugger). It adds an Stop-Process foo* -ErrorAction BreakThat command will automatically enter the debugger on terminating or non-terminating error, and from that point you can check the call stack, step, continue execution, or terminate (quit) the script that is running. Once that is merged, it will resolves some of the needs identified here (breaking into the debugger and/or checking the callstack on error). |


jpsnover commentedJan 24, 2018
•
edited by rjmholt
Ubiquitous -OnError {Scriptblock} parameter which would take precedence over -ErrorAction and others. The scriptbock would be called on terminating errors and would be called for every non-terminating error. For non-terminating errors, the scriptblock can control whether to STOP or CONTINUE code execution.
This would unify the processing of terminating vs non terminating errors: