Allow ||
to override $PSNativeCommandUseErrorActionPreference = $true
, for ad-hoc error handling / ignoring nonzero exit codes
#20067
Labels
Issue-Enhancement
the issue is more of a feature request than a bug
Needs-Triage
The issue is new and needs to be triaged by a work group.
Summary of the new feature / enhancement
$PSNativeCommandUseErrorActionPreference = $true
currently cannot be overridden for individual calls, even though that would be handy to deal with (a) anticipated failures and (b) non-conventional CLIs such asrobocopy.exe
andchoice.exe
that use nonzero exit codes to (also) signal success conditions.Analogous to how you can use
||
in Bash to override its roughly equivalent setting (set -e
; e.g,bash -c 'set -e ; ls nosuch || : ; echo still here'
), PowerShell would benefit from this ability too.That is:
If a native-program call is the LHS of the
||
operator, do not emit theSystem.Management.Automation.NativeCommandExitException
-based error when$PSNativeCommandUseErrorActionPreference = $true
is in effect.This allows either custom error handling via the RHS operand, or ignoring the nonzero exit code altogether via
@()
(which is in effect a no-op, similar to Bash's:
builtin, though, unlike the latter,@()
does not reset$LASTEXITCODE
).Example:
Conceptually related proposal (whose scope is larger) that would solve the problem differently (
Invoke-NativeCommand { ls nosuchfile } -IgnoreExitCode
):Proposed technical implementation details (optional)
No response
The text was updated successfully, but these errors were encountered: