- 1 year ago
Rundeck - PowerShell exit code
In Rundeck, you can capture the exit code of a PowerShell script and use it to determine the success or failure of the script execution. Rundeck provides a variable called ${exec.exitCode}
that holds the exit code value of the last executed command or script.
Here's an example of how you can use the exit code in Rundeck with a PowerShell script:
Create a new Rundeck Job:
- In the Rundeck web interface, navigate to the project where you want to create the job.
- Click on "Jobs" and then "Create a New Job".
- Define the necessary details for the job, such as the name, description, and node(s) where the PowerShell script will be executed.
Configure the Step to Run a PowerShell Script:
- Add a new step to the job.
- Select "Script" as the Step Type.
- Choose the appropriate node where the PowerShell script will be executed.
Write the PowerShell Script:
- In the "Script" section, write your PowerShell script.
- Make sure the script returns an exit code that indicates success (
0
) or failure (a non-zero value) based on your script's logic.
For example, here's a simple PowerShell script that exits with an exit code of
0
:powershell# PowerShell script that exits with exit code 0 (success)
exit 0
Capture and Use the Exit Code in Rundeck:
- In the "Script" section, add a line to capture the exit code using the
$LASTEXITCODE
variable. - You can then use the
${exec.exitCode}
variable in subsequent steps or notifications to determine the success or failure of the PowerShell script execution.
For example, in Rundeck, you can use the
${exec.exitCode}
variable like this:ruby${if exec.exitCode == 0}Success!${else}Failure!${endif}
In this example, if the exit code is
0
, it will print "Success!", otherwise it will print "Failure!".- In the "Script" section, add a line to capture the exit code using the
By utilizing the ${exec.exitCode}
variable in Rundeck, you can capture the exit code of a PowerShell script and use it for conditional logic or in subsequent steps to determine the success or failure of the script execution.