none
Help returning custom exit codes when running a powershell script RRS feed

  • Question

  • I have an exe application that i use at work and I found that i can do a task faster with powershell. I wrote a script that the application calls but Iam having issues getting the exit code returned. At first I thought it was the exe but i found that if i called the script in the command prompt i didnt get the exit code either.

    I have searched around the internet and I thought i was doing this right but i guess not. for testing i created a simple script that had the below information in it.

    $ExitCode = "123"
    Exit $ExitCode

    Once I had it save i opened the command prompt and ran the below code

    Powershell.exe -file "C:\exitcode.ps1"

    When it runs i get nothing in return, if someone could show me what im doing wrong that would be great, I have also tried calling this as a command instead of a file.

    Thursday, July 31, 2014 9:41 PM

Answers

  • The question here was "exit 123"  not setting a return code which it does.  The onenct article is about throwing an exception and having powershell default to a non zero EXITCODE.

    Even compiled program exhibit this behavior.  Detecting an exception and setting a default exit code is not a specifi Windows standard in my experience.  It is a nicety that we should do.  Some programs set severity codes which are defined in the SDK in batches - like 1366 and 1700 - don't ask me to look up the exact codes but they are, at time, high numbers.

    The important issue, I believe, is that this is not standard or required but I am surprised that PowerShell does not do this.


    ¯\_(ツ)_/¯

    Friday, August 1, 2014 12:38 AM

All replies

  • Looks like this is a known problem.

    https://connect.microsoft.com/PowerShell/feedback/details/777375/


    -- Bill Stewart [Bill_Stewart]

    Thursday, July 31, 2014 9:52 PM
    Moderator
  • Thanks I will find another way to get this to work then. I hope they get it fixed.
    Thursday, July 31, 2014 9:56 PM
  • It doesn't do that in PowerShell V4.


    ¯\_(ツ)_/¯

    Thursday, July 31, 2014 11:30 PM
  • It doesn't do that in PowerShell V4.


    ¯\_(ツ)_/¯

    It seems to for me, using the example from the Connect report:

    C:\Temp>type test.ps1
    throw "Help"
    
    C:\Temp>echo %errorlevel%
    0
    
    C:\Temp>powershell -file "c:\temp\test.ps1"
    Help
    At C:\temp\test.ps1:1 char:1
    + throw "Help"
    + ~~~~~~~~~~~~
        + CategoryInfo          : OperationStopped: (Help:String) [], RuntimeExcep
       tion
        + FullyQualifiedErrorId : Help
    
    
    C:\Temp>echo %errorlevel%
    0
    
    C:\Temp>powershell -command "Write-Output $psversiontable"
    
    Name                           Value
    ----                           -----
    PSVersion                      4.0
    WSManStackVersion              3.0
    SerializationVersion           1.1.0.1
    CLRVersion                     4.0.30319.18444
    BuildVersion                   6.3.9600.16406
    PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0}
    PSRemotingProtocolVersion      2.2



    Don't retire TechNet! - (Don't give up yet - 12,950+ strong and growing)

    • Edited by Mike Laughlin Friday, August 1, 2014 12:16 AM Add version output
    Friday, August 1, 2014 12:11 AM
  • The question here was "exit 123"  not setting a return code which it does.  The onenct article is about throwing an exception and having powershell default to a non zero EXITCODE.

    Even compiled program exhibit this behavior.  Detecting an exception and setting a default exit code is not a specifi Windows standard in my experience.  It is a nicety that we should do.  Some programs set severity codes which are defined in the SDK in batches - like 1366 and 1700 - don't ask me to look up the exact codes but they are, at time, high numbers.

    The important issue, I believe, is that this is not standard or required but I am surprised that PowerShell does not do this.


    ¯\_(ツ)_/¯

    Friday, August 1, 2014 12:38 AM
  • I have an exe application that i use at work and I found that i can do a task faster with powershell. I wrote a script that the application calls but Iam having issues getting the exit code returned. At first I thought it was the exe but i found that if i called the script in the command prompt i didnt get the exit code either.

    I have searched around the internet and I thought i was doing this right but i guess not. for testing i created a simple script that had the below information in it.

    $ExitCode = "123"
    Exit $ExitCode

    Once I had it save i opened the command prompt and ran the below code

    Powershell.exe -file "C:\exitcode.ps1"

    When it runs i get nothing in return, if someone could show me what im doing wrong that would be great, I have also tried calling this as a command instead of a file.

    Here is a reminder of the original question which I cannot reproduce.


    ¯\_(ツ)_/¯

    Friday, August 1, 2014 12:41 AM
  • The question here was "exit 123"  not setting a return code which it does. 

    Ah, yes, so it was.

    Oh well, that was fun anyway.

    C:\Temp>echo %errorlevel%
    0
    
    C:\Temp>powershell -file "c:\temp\test.ps1"
    
    C:\Temp>echo %errorlevel%
    123
    
    C:\Temp>type test.ps1
    Exit 123


    Don't retire TechNet! - (Don't give up yet - 12,950+ strong and growing)

    Friday, August 1, 2014 2:17 AM
  • I just tested also and can't reproduce it. As jrv correctly pointed out, the connect bug is referring to the throw statement and not exit.

    From cmd.exe prompt:


    C:\>type test.ps1
    exit 21
    C:\>echo %ERRORLEVEL%
    0
    C:\>powershell.exe -file test.ps1
    C:\>echo %ERRORLEVEL%
    21
    

    This is Windows 7 and PowerShell 3.


    -- Bill Stewart [Bill_Stewart]

    Friday, August 1, 2014 2:43 AM
    Moderator