Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

powershell

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?

like image 966
ripper234 Avatar asked Sep 08 '09 13:09

ripper234


2 Answers

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.

like image 90
Colonel Panic Avatar answered Oct 06 '22 00:10

Colonel Panic


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.

like image 31
ripper234 Avatar answered Oct 05 '22 22:10

ripper234