0

Say I have two files in a directory with angle brackets in the path (e.g. c:\foo[bar])

  • test.ps1
  • test.bat

In test.ps1 all I have is:

"HelloWorld"

In test.bat I have:

powershell -file test.ps1
echo %errorlevel%

Where powershell v2 is installed, if I execute test.bat from the command prompt, with the working directory in the foo[bar], I get "HelloWorld" output from the ps script but the error code 1 echoed to the console. This does not occur when the path contains no angle brackets or in instances where powershell v3 is installed ("HelloWorld" is output and 0 is returned as the error code).

This just appears to be a problem when using the powershell v2 executable with the -file switch. My first assumption is that this is a bug, but I couldn't find anything out there on this specific problem.

Is there any workaround or solution to this?

Pero P.
  • 25,813
  • 9
  • 61
  • 85
  • 1
    Actually I think you're running into a bug of V3 that is causing the exit code to always be 0. See https://connect.microsoft.com/PowerShell/feedback/details/750653/powershell-exe-doesn-t-return-correct-exit-codes-when-using-the-file-option The workaround is to use the -command parameter instead of -file. – Keith Hill Jul 16 '14 at 02:31

1 Answers1

1

You should probably have the script itself specify its exit code. I would start by having a read of this: http://weblogs.asp.net/soever/returning-an-exit-code-from-a-powershell-script but the top comment from Keith Hill is probably more specific to your case.

BSAFH
  • 725
  • 3
  • 6
  • 19
  • Actually this solves the problem for both v2 and v3. If I explicitly end the script with `exit 0` then I get the right error code in the bat file. Thanks! – Pero P. Jul 16 '14 at 17:25