Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Error handling by JobScheduler

Checking the exit code of PowerShell script

  • PowerShell scripts can use the exit() function to stop execution and to signal the execution result by specifying
    • exit(0) for successful execution
    • exit(1) or any non-zero numeric value
  • JobScheduler will report the value of the exit() function as the job execution result.

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. 
    • The same is true for neIf a number of programs/scripts are executed from a PowerShell job then the result of the last program/script will decide about the exit code stored with $LastExitCode.
    • This behavior does not apply to cmdlets causing errors.
    • The behavior to stop script execution in case of an error by use of the $ErrorActionPreference does not apply to errors caused by programs/scripts but to PowerShell commands and cmdlets only.
  • 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%).

...

  • 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)
      • 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).
  • Users should consider that output checking is performed after the job has completed. It can therefore not be used to stop execution of a script in case of error, however, we recommend to use output checking that would set the execution result of jobs in case of errors that are not handled by the script.
  • For details check 
    Jira
    serverSOS JIRA
    columnskey,summary,type,created,updated,due,assignee,reporter,priority,status,resolution
    serverId6dc67751-9d67-34cd-985b-194a8cdc9602
    keyJS-1329
     and 
    Jira
    serverSOS JIRA
    columnskey,summary,type,created,updated,due,assignee,reporter,priority,status,resolution
    serverId6dc67751-9d67-34cd-985b-194a8cdc9602
    keyJS-1393

...

  • 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. It is recommended to set this variable in the PowerShell Profile, see chapter Profile Handling.
    • 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.

...