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.
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.
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
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