I have a script that runs an external EXE file. When that EXE file fails (sets errorlevel to 1), the PowerShell script fails.
I'm running curl.exe and am getting this:
- CategoryInfo : NotSpecified: ( % Total % ... Time Current:String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError
How can I ignore/catch the failure of the external EXE file and continue with my script?
Actually, the application ran fine - PowerShell is mistaken in reporting an error.
When an application prints to standard error, PowerShell will sometimes conclude that application has failed. This is actually a design decision made by PowerShell developers. IMHO, this is a mistake, because many reliable applications (such as curl) print useful information to standard error in the course of normal operation. The consequence is that PowerShell only plays well with other PowerShell scripts and can't be relied on to interoperate with other applications.
Other readers in this thread had difficulty reproducing the behaviour because PowerShell implements it inconsistently. Whether the NativeCommandError occurs depends on how standard error is redirected (as a consequence the bug occurs in vanilla PowerShell ISE, but not vanilla PowerShell).
Whatever your opinion of the design decision in the first paragraph, the inconsistent implementation is for certain a PowerShell bug - see $LastExitCode=0 but $?=False in PowerShell. Redirecting stderr to stdout gives NativeCommandError.
I was running the script through PowerShell ISE (an IDE), and I believe this is what caused the problems.
Running it through PowerShell itself seems to work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With