Versions Compared

Key

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

Table of Contents
outlinh1. true
outlinh1. true
1printablefalse
2stylh1. none
3indent20px

Exit Code Handling with PowerShell

The exit - code of a PowerShell script is normally usually expected as the result of the execution of the script. However, but the program powershell-.exe returns the exit - code of the execution of the PowerShell powershell.exe itself - and this in most cases 0.

To get the "real" exitcode retrieve the effective exit code of the script, the that script has to be terminated with an "exit" statement () function and as parameter the variable "lastexitcode" $lastexitcode (or any other value/variable for the exitcodeexit code).

The example below shows how it works:

Code Block

 <?xml version="1.0" encoding="ISO-8859-1"?>
 <job title="How to get the exit exitcodecode from a powershell script"
     name="PowershellExitCodePowerShellExitCode">
    <script language="shell">
        <![CDATA[
 powershell.exe -noprofile -command "write-output test; exit 123 "
 echo %errorlevel%
 exit %errorlevel%
        ]]>
     </script>
     <run_time/>
 </job>

 

The result of this job is visible in the log - file:

Code Block

 [debug9] SCHEDULER-918  state=running_process (never)
  [info]   C:\Program Files (x86)\Scheduler>powershell -noprofile -command "write-output test; exit 123 " 
 [info]   test 
 [info]   C:\Program Files (x86)\Scheduler>echo 123  
 [info]   123 
 [info]   C:\Program Files (x86)\Scheduler>exit 123 
 [ERROR]  SCHEDULER-280  Process terminated with exit code 123 (0x7B)

The statement "exit %errorlevel%" tells JobScheduler passes the exit code to JobScheduler. If this statement is missing , JobScheduler knows nothing then JobScheduler will not know about the exit code and will assume that it is zero (0).(see

See also

...

...

see also:

...

  •  

 

 

...