Return Codes to automation engine

Welcome Forums General PowerShell Q&A Return Codes to automation engine

This topic contains 1 reply, has 2 voices, and was last updated by

 
Participant
1 month, 1 week ago.

  • Author
    Posts
  • #119640

    Participant
    Points: 48
    Rank: Member

    Good day all,

    I am migrating all of my install scripts to PowerShell for our migration to Workspace One/Airwatch.  In my mind, the answer should be the same regardless of the management platform.  I have built in error codes into my script based on what failed.  In the past I have used:

    exit $lastexitcode #or

    exit 1

    A developer friend said I should use:

    $host.SetShouldExit(0) #where 0 is the number you want to return

    Poking around I found (https://serverfault.com/questions/716760/how-to-get-sccm-to-recognize-return-codes-from-powershell-script-completion/716824)
    [System.Environment]::Exit(0)

    Instead of mindlessly stealing code I would like to understand why I should use what I am using.

  • #119716

    Participant
    Points: 190
    Helping Hand
    Rank: Participant

    $Host.SetShouldExit() and [System.Environment]::Exit(), kind of looks like they are doing the same thing.
    Not sure if they use the same call or not but you can probably find that out on MSDN.

    Using Exit seem to only produce a 0 if you Exit 0, and 1 for any non-zero values.
    At least if you check the %errorlevel% or $LASTEXITCODE variable afterwards.

    So from a sense that you want to use more than 0 or 1 as exit code then it makes sense to use one of the others.
    Read somewhere that $Host.SetShouldExit() can be unpredictable, haven't test it myself that much so I can't really say.

You must be logged in to reply to this topic.