Getting an exit code from an external PowerShell script

Paul Watson (101 posts)
December 30, 2016 08:33 PM
Accepted Answer

What is the correct way to return a non-zero exit code from a PowerShell script?

Using "return 7" causes adTempus to indicate that the return code was zero (0).

Using "exit 7" causes adTempus to indicate that the return code was one (1).

I found that I could use $host.SetShouldExit(7) to set the exit code in a PowerShell script. However, this also terminates the console host when testing interactively.

Is there a good solution? Is there any particular part of the adTempus documentation I should be reading?

Bill Staff (599 posts)
December 31, 2016 09:19 AM
Accepted Answer

You should be able to use "exit 7" and get the correct result. $host.SetShouldExit will also work.

How are you running the script--using a Script Task? Using a Program Execution Task to run PowerShell.exe explicitly? Through a batch file? Through nested scripts?

There's a long discussion here about various issues with exit codes from PowerShell.

Paul Watson (101 posts)
December 31, 2016 12:31 PM
Accepted Answer

I was running as an external program; using "powershell.exe" as the "Target" and "-NoProfile .\Get-Mine.ps1" as the "Command-Line Parameters." Yes, I set "C:\Users\pwatson\t" as the "Startup Directory."

In this case, using "exit 7" caused adTempus to report that the job exit code was one (1). That is the same as reported in the web page you mentioned.

I have now tried createing the script as "Basic Tasks > Execute a script." This seems to be working more like what I was expecting.

What I would like to do is use a PowerShell command in the adTempus job and not an external .ps1 file. Is that possible?

Bill Staff (599 posts)
January 2, 2017 09:51 AM
Accepted Answer

I think I misread my results--I am getting the same behavior as you when using "exit 7" and calling powershell.exe as an external program.

I'm not clear what you mean by "use a PowerShell command in the adTempus job and not an external .ps1 file." In your Script Task, you can configure it to execute a "script stored in adTempus" instead of an external script, and use a PowerShell script stored internally, just like you would a VB.NET script. Is that what you're looking for?

Paul Watson (101 posts)
January 2, 2017 10:06 AM
Accepted Answer

I have a PowerShell module that exposes a function, MyFunc().

H:>type MyUtils.psm1
function MyFunc()
{
    [cmdletbinding()]
    Param()
 
    Write-Host "now from myfunc"
    return 8
}

I thought that I could use the adTempus Step "Execute a script" to run the 'MyFunc" command. However, the adTempus step wants an external file name.

If I save a script inside adTempus that calls "MyFunc", would that work?

Does adTempus automatically include "-NonInteractive" when invoking PowerShell?

Bill Staff (599 posts)
January 2, 2017 12:29 PM
Accepted Answer

When you create a step to "Execute a script" the Script Execution Task properties window gives you two choices:

  • Execute a script stored in adTempus
  • Execute a script stored in an external file

Using the first option you can create a new script and put in code that calls the function from your module. I believe you will need to include the code to explicitly load the module first.

When you use a Script Execution Task adTempus does not invoke powershell.exe. It runs its own PowerShell script host to execute the script. This host doesn't support any user interaction, so it is like running powershell.exe with the -NonInteractive option. Any output that the script writes will go to the "PowerShell Output" file saved in the Captured Files for the job instance.

Paul Watson (101 posts)
January 2, 2017 12:38 PM
Accepted Answer

Let's see if I understand. I must write a script that is stored in adTempus or in an external file.

I cannot just write a PowerShell statement to invoke a function that is available. I have to write a cmdlet script that invokes the function.

Bill Staff (599 posts)
January 2, 2017 12:54 PM
Accepted Answer

Your PowerShell "statement" has to be inside a "script".

You can do this:

  1. Create a step to "Execute a script"
  2. On the Script page of the settings, select "Execute a script stored in adTempus"
  3. Click the New button to open the Script Editor
  4. Change the Language to Windows PowerShell
  5. Add a line to use Import-Module to load your module
  6. Add a line to call your function

That's all you need.

Paul Watson (101 posts)
January 2, 2017 01:02 PM
Accepted Answer

Ok, Bill. I think I get it. The script to call the function can exist in adTempus or an external .ps1 file.

If I already have the module containing the exported function in a profile directory, I don't think I need to explicitly import the module.

Replies are disabled for this topic.