Skip to content

Job using PowerShell runtime

The following example shows how to create a job using the PowerShell runtime agents.

Check

If using no-auth mode you need to ensure that you use throw for failure.

$expectedValue = 'test123!'

if ($env:userParam) {

    if ($userParam -ne $expectedValue) { 
        throw "User parameter does not match expected value."
    } else { 
        Write-Host "User parameter matches expected value."
    }
} else {
    throw "User parameter is not set."
}

Do

The script can produce any result you want, the output will be returned to the caller using stdout.

Write-Output "Success"

Set PowerShell mode

All these options will automatically result in the Job being stopped.

$ErrorActionPreference = 'Stop'                  # stop the script on any error
Set-StrictMode -Version Latest                   # error on unset/misspelled variables
$PSNativeCommandUseErrorActionPreference = $true # failed commands stop the script (v7.3+ only)

Logging

If you worker is set to keep Job run directories then you can log the output of your script with the following.

# Trace script exection to a log file
# Default location: C:\ProgramData\ReACKsWorker\run\JOBID
Start-Transcript -Path "job.log" -Append | Out-Null

# Stop logging
Stop-Transcript | Out-Null