Automic Workload Automation

 View Only
  • 1.  PowerShell Exit Code

    Posted Sep 17, 2018 05:55 PM

    I'm attempting to retrieve an exit code from a PowerShell function and then exit with that code from an Automic JOB. I've confirmed that the function does exit with the appropriate exit code, it's just not getting back out to the JOB. Here's an example of the 'Process' tab on the JOB.

     

    :BEGIN_EXT_INT POWERSHELL
    Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope Process -Confirm:$false -Force

    Import-Module -Name "\\networkShare\customModule.psm1"

    $VerbosePreference = "Continue"

    Custom-Function -Verbose

    :REGISTER_VARIABLE EXIT_CODE#, $LastExitCode

    :END_EXT_INT POWERSHELL

    :Exit &EXIT_CODE#

     

    When I run this, I receive an error:

    Syntax error in object 'JOBS...' line '00014'. 'U01001308 Variable 'EXIT_CODE#' has not yet been defined.



  • 2.  Re: PowerShell Exit Code

    Posted Sep 19, 2018 09:10 AM

    Hi

     

    Thats an "execution-issue".

     

    Automic Script is always processed prior to any JCL.

    So the :Exit &EXIT_CODE# is processed before the variable &EXIT_CODE# is set in your PS section.

    Its seen as Automic Script. If you remove the colon it will be interpreted as PS command.

     

    Suggestion (not tested)

    :REGISTER_VARIABLE EXIT_CODE#, $LastExitCode

    exit $LastExitCode

    :END_EXT_INT POWERSHELL

     

    cheers, Wolfgang



  • 3.  Re: PowerShell Exit Code

    Posted Sep 24, 2018 05:17 PM

    Unfortunately, this did not work. It still exited 'ENDED_OK - ended normally'. In this situation, I confirmed (via Verbose Output on the script) that the exit code was 1, so it should have ended in an error (note I did also confirm that the General > Runtime > Return code <= setting is 0). 



  • 4.  Re: PowerShell Exit Code

    Posted Sep 20, 2018 11:21 AM

    For Powershell error trapping, I use this Powershell syntax to force it to quit and send RC=1 back to Automic immediately;

     

    $RC = $?
    switch ($RC)
    {
    True { $EXIT_CODE = 0 }
    False { $EXIT_CODE = 1 ; echo "Aborting script!" ; exit $EXIT_CODE }
    default { $EXIT_CODE = 1 ; echo "Aborting Script!" ; exit $EXIT_CODE }
    }



  • 5.  Re: PowerShell Exit Code

    Posted Sep 24, 2018 05:15 PM

    Is this in the PowerShell code (between :BEGIN_EXT_INT POWERSHELL and :END_EXT_INT POWERSHELL)?



  • 6.  Re: PowerShell Exit Code

    Posted Sep 24, 2018 08:26 PM

    Yes my suggested code goes between the :BEGIN and :END.  It is PowerShell logic.



  • 7.  Re: PowerShell Exit Code
    Best Answer

    Posted Sep 26, 2018 12:43 PM

    I ended up switching this to executing a .ps1 file with the following lines at the end:

     

    @set retcode=%errorlevel%
    @if NOT %ERRORLEVEL% == 0 goto :retcode