Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get powershell to return the correct exit code when called with the -File argument?

Powershell is returning a 0 exit code, when an error has occurred, if called with the -File argument. Which means my build is green when it shouldn't be :(

For example:

(in wtf.ps1)

$ErrorActionPreference = "Stop";   
$null.split()

(cmd)

powershell -file c:\wtf.ps1  
You cannot call a method on a null-valued expression.
At C:\wtf.ps1:3 char:12
+ $null.split <<<< ()
    + CategoryInfo          : InvalidOperation: (split:String) [], ParentConta
   insErrorRecordException
    + FullyQualifiedErrorId : InvokeMethodOnNull


echo %errorlevel%  
0

powershell c:\wtf.ps1  
You cannot call a method on a null-valued expression.
At C:\wtf.ps1:3 char:12
+ $null.split <<<< ()
    + CategoryInfo          : InvalidOperation: (split:String) [], ParentConta
   insErrorRecordException
    + FullyQualifiedErrorId : InvokeMethodOnNull


echo %errorlevel%  
1

Any ideas?

(I've tried pretty much every idea from the first 2 pages of this: https://www.google.co.uk/search?q=powershell+file+argument+exit+code already)

like image 308
grahamrhay Avatar asked May 16 '12 14:05

grahamrhay


People also ask

How do I return an exit code in PowerShell?

Use the command Exit $LASTEXITCODE at the end of the powershell script to return the error codes from the powershell script. $LASTEXITCODE holds the last error code in the powershell script.

Does Return exit the function PowerShell?

The return keyword exits a function, script, or script block. It can be used to exit a scope at a specific point, to return a value, or to indicate that the end of the scope has been reached.

How do I step through a PowerShell script?

Press F5 or, on the toolbar, click the Run Script icon, or on the Debug menu, click Run/Continue or, in the Console Pane, type C and then press ENTER . This causes the script to continue running to the next breakpoint or to the end of the script if no further breakpoints are encountered.

How do I go back to C in PowerShell?

The Windows PowerShell prompt opens by default at the root of your user folder. Change to the root of C:\ by entering cd c:\ inside the Windows PowerShell prompt.


2 Answers

In the script, use the exit keyword with a number of your choice:

exit 34

Here's the script I used to test this:

## D:\Scripts\Temp\exit.ps1 ##
try{
    $null.split()
}
catch
{
    exit 34
}

exit 2
#############################

# launch powershell from cmd 
C:\> powershell -noprofile -file D:\Scripts\Temp\exit.ps1
C:\>echo %errorlevel%
34
like image 193
Shay Levy Avatar answered Oct 16 '22 00:10

Shay Levy


It is a known problem. Workarounds are calling the script with -File, using the -Command parameter (and adding ; exit $lastexitcode if you also have your own exit codes) or turning them into exit codes like Shay is showing or the example using trap below. See here for more information.

trap
{
    $ErrorActionPreference = "Continue";   
    Write-Error $_
    exit 1
}

$ErrorActionPreference = "Stop";   
$null.split()
like image 21
Lars Truijens Avatar answered Oct 16 '22 00:10

Lars Truijens