ESP Workload Automation

 View Only
  • 1.  PowerShell script exit not recognized by ESP NT_JOB

    Posted May 19, 2025 02:13 PM

    Is there a way to have ESP recognize my coded exit number in a PowerShell script?

    I'm trying to have an ESP NT_JOB run a script on a Windows 2019 server and act upon the exit code in my PowerShell script.

    The NT_JOB seems to only see the end of the PowerShell script as a 0 or 1

    In this primitive example I end the script with exit 777.
    The ESP job executing the script sees Completion Code CC 1 rather than 777

     CMDNAME powershell  
     ARGS e:\Scripts\return-code-to-for-esp-use.ps1

    Example PowerShell script

    Write-Output "Going to exit with 777";
    exit 777
    ^^^^^^^^^^^^^^^
    Ran from PowerShell on the server
    PS C:\Users\Loren> E:\Scripts\return-code-to-for-esp-use.ps1
    Going to exit with 777
     
    PS C:\Users\Loren> $?
    True
     
    PS C:\Users\C3829B> $LASTEXITCODE
    777
     
    PS C:\Users\C3829B> 


    ------------------------------
    Loren Watts
    Cigna Healthcare
    ------------------------------


  • 2.  RE: PowerShell script exit not recognized by ESP NT_JOB

    Posted May 19, 2025 07:56 PM

    I added a Function to the PowerShell script, then called it before exiting the script.

    Function

    #-----------------------------------------------------------------------------------#
    #   Functions ExitWithCode                                                          #
    #-----------------------------------------------------------------------------------#

    function ExitWithCode
    {
        param
        (
            $exitcode
        )

        $host.SetShouldExit($exitcode)
        exit

    Program Excerpt

    #####################################################################################
    #                                                                                   #
    #   Main Program                                                                    #
    #                                                                                   #
    #####################################################################################

    $global:ExitStatus = 0

    .

    .

    .

    ExitWithCode -exitcode $ExitStatus




  • 3.  RE: PowerShell script exit not recognized by ESP NT_JOB

    Broadcom Employee
    Posted May 20, 2025 08:34 AM

    Hi Rick,
     
    So nice to "see" you again.

    Thank you for your input as always,


    Lucy






  • 4.  RE: PowerShell script exit not recognized by ESP NT_JOB

    Broadcom Employee
    Posted May 21, 2025 09:16 AM

    I second that. When I saw Rick had responded, it put a smile on my face. 
    Loren, you are asking some interesting questions. Love to see it. 




  • 5.  RE: PowerShell script exit not recognized by ESP NT_JOB

    Posted May 20, 2025 10:24 AM

    Thank you Rick for sharing your time and knowledge. I'll give this idea try.



    ------------------------------
    Loren Watts
    Cigna Healthcare
    ------------------------------



  • 6.  RE: PowerShell script exit not recognized by ESP NT_JOB

    Posted May 21, 2025 02:11 PM
      |   view attached

    I have attached a complete PowerShell script that exits with whatever parameter is passed to it.  If no parameter is passed it exits with 0.

    I recommend setting up an NT_JOB with USER1 as an Argument then you can test through ESP.

    Attached file is saved with a .txt extension, change extension to ps1 before running


    Attachment(s)

    txt
    ExitCodeTest.txt   1 KB 1 version