...
PowerShell jobs offer a number of options to detect run-time errors.
Error handling by JobScheduler
Checking the last exit code of a Windows program or script
- If a native Windows program or script (.bat, .cmd) causes an error then this will assign the exit code to the
$LastExitCode
global variable. This behavior does not apply to cmdlets causing errors. - JobScheduler checks this variable and will set the job exit code accordingly. For exit codes != 0 an error is raised.
- This behavior is superior compared to shell jobs as errors from any line in the job script are detected. For shell scripts only the last line of a job script determines the execution result (Unix:
$?
, Windows:%ERRORLEVEL%
).
Checking
...
error output
- Native Windows programs or scripts and cmdlets can cause errors that create some error output (stderr).
- PowerShell jobs can be configured to handle any output found in stderr to indicate an error. Not all output to stderr may indicate an error as a number of Windows native programs is reported to write to stderr instead of stdout. Therefore users can choose from the following configuration:
<job stderr_log_level="error"/>
- Indicates that any output found in stderr will result in a job error.
<job stderr_log_level="info"/>
- Indicates that output to stderr is logged but will not result in a job error (default).
- For details check
andJira server SOS JIRA columns key,summary,type,created,updated,due,assignee,reporter,priority,status,resolution serverId 6dc67751-9d67-34cd-985b-194a8cdc9602 key JS-1329
- By default PowerShell most cmdlets write errors to the stderr output channel that is captured by JobScheduler.
- This does not include that a processing error is raised as in this situation the next line of the PowerShell job script would be executed.
- Users can force PowerShell to throw an exeception in case of cmdlet errors
- by setting the global variable
$ErrorActionPreference = "Stop"
. This would stop processing immediately and prevent subsequent lines of the PowerShell job script from being executed. The value of this variable will be considered in case of cmdlet errors and in case of syntax errors, e.g. when using wrong parameterization for cmdlets. JobScheduler will detect this type of error. - by setting the
-ErrorAction stop
parameter provided that the cmdlet supports common parameters. The-ErrorAction
parameter is not considered for syntax errors when using cmdlets, e.g. in case of wrong parameterization.
- by setting the global variable
Checking error output
- Native Windows programs or scripts and cmdlets can cause errors that create some error output (stderr).
- PowerShell jobs can be configured to handle any output found in stderr to indicate an error. Not all output to stderr may indicate an error as a number of Windows native programs is reported to write to stderr instead of stdout. Therefore users can choose from the following configuration:
<job stderr_log_level="error"/>
- Indicates that any output found in stderr will result in a job error.
<job stderr_log_level="info"/>
- Indicates that output to stderr is logged but will not result in a job error (default).
- For details check
andJira server SOS JIRA columns key,summary,type,created,updated,due,assignee,reporter,priority,status,resolution serverId 6dc67751-9d67-34cd-985b-194a8cdc9602 key JS-1329 Jira server SOS JIRA columns key,summary,type,created,updated,due,assignee,reporter,priority,status,resolution serverId 6dc67751-9d67-34cd-985b-194a8cdc9602 key JS-13931393
Error handling by job scripts
Checking cmdlet errors
- By default PowerShell most cmdlets write errors to the stderr output channel that is captured by JobScheduler.
- This does not include that a processing error is raised as in this situation the next line of the PowerShell job script would be executed.
- Users can force PowerShell to throw an exeception in case of cmdlet errors
- by setting the global variable
$ErrorActionPreference = "Stop"
. This would stop processing immediately and prevent subsequent lines of the PowerShell job script from being executed. The value of this variable will be considered in case of cmdlet errors and in case of syntax errors, e.g. when using wrong parameterization for cmdlets. JobScheduler will detect this type of error. - by setting the
-ErrorAction stop
parameter provided that the cmdlet supports common parameters. The-ErrorAction
parameter is not considered for syntax errors when using cmdlets, e.g. in case of wrong parameterization.
- by setting the global variable
Suppressing Exit Codes
- Users who wish to suppress exit codes of failed executions of native Windows programs or scripts can use
$LastExitCode = $null
- This will disable the above mentioned check of the last exit code.
Catching errors
- Users can use
try/catch
blocks to implement individual error handlingThis simple example prevents errors from being raised for non-accessible network shares:
Code Block language powershell try { $found = Test-Path \\some_share\some_directory\some_file -PathType Leaf } catch { $found = $false }
- From the above example an exception is raised and handled that does not result in a job error. In addition no output to stderr is created that would be detected by the above mentioned error output checking.
...