Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Powershell forget to set ERRORLEVEL on parser errors?

In a command prompt, Try this out:

powershell aaa

echo ErrorLevel is %errorlevel%

Powershell will fail (with an error). You will also see "ErrorLevel is 1". This is because it runs aaa as a script, which makes an unhandled error, which sets the errorlevel.

However, now try this:

powershell '

echo ErrorLevel is %errorlevel%

Powershell will fail (with an error). This time, you will also see "ErrorLevel is 0".

I realize it's a different problem. Powershell even will say it's a ParserError, and it's because I screwed up the command line arguments. Still, that's not cool.

I am unclear as to why it behaves this way. I'm guessing this was an oversight. Other programs out there neglect setting errorlevel on failures, but in this case it's as if they went halfway. It's even setting it to 0 here as opposed to not setting it at all.

like image 289
Johnny Kauffman Avatar asked Dec 15 '11 19:12

Johnny Kauffman


People also ask

What is ErrorLevel in PowerShell?

In Cmd.exe, %ErrorLevel% is a builtin variable which indicates the success or failure of the last executable run. In PowerShell, we support: $? Contains True if last operation succeeded and False otherwise.


1 Answers

Hmm I think I agree that a parse error should set the return code to non zero. I suggest filing this over at the the Powershell connect site. http://connect.microsoft.com/PowerShell

like image 181
Andy Arismendi Avatar answered Nov 07 '22 17:11

Andy Arismendi