Ignoring an errorlevel != 0 in Windows PowerShell (ISE)

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?


Solution 1:

This doesn't have anything to do with the exit code returned by the EXE. An error is generated when an EXE writes to stderr, but only within the ISE or when remoting or using background jobs.

An EXE that writes to stderr does not generate errors from the regular PowerShell command prompt. I'm not sure why this is the case.

Solution 2:

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.