You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Next »

The exit-code of an powershell script is normaly expected as the result of the execution of the script, but the powershell-exe returns the exit-code of the execution of the powershell.exe itself - and this in most cases 0.

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

The example below shows how it works:

 <?xml version="1.0" encoding="ISO-8859-1"?>
 <job title="How to get the exitcode from a powershell script"
     name="PowershellExitCode">
    <script language="shell">
        <![CDATA[
 powershell -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:

 [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 the exit code. If this statement is missing, JobScheduler knows nothing about the exit code and will assume that it is zero (0).

(see also
[ here|http://powershell.com/cs/blogs/tips/archive/2009/05/18/returning-exit-code-from-script.aspx]
for more details.

see also:

  • No labels