title: Windows Task Scheduler reports incorrect/inconsistent result code
body: <p><strong>Background:</strong> I am attempting to get email notification upon failure of a scheduled task. My task can indicate failure via exit code (errorlevel) and I want to use that and follow the filter approach described in <a href="https://stackoverflow.com/a/4289360/29805">this answer</a> to trigger an email.</p>
  
  <p><strong>Problem:</strong> I am getting inconsistent behavior from Task Scheduler on different machines and versions of Windows. To test, I'm using the following very simple task.</p>
  
  <ul>
  <li>Run only when user is logged on. (where "user" is the current user)</li>
  <li>Action: Start a program
  
  <ul>
  <li>Program/script: <code>cmd</code></li>
  <li>Arguments: <code>/c "exit /b 1"</code></li>
  <li>Start in: blank</li>
  </ul></li>
  <li>Everything else: default</li>
  </ul>
  
  <p>I've enabled Task History, and when I manually run the task on both Windows 7 Ultimate and Windows Server 2008 R2 Enterprise, a correct history item like the following is created in category "Action completed":</p>
  
  <blockquote>
    <p>Task Scheduler successfully completed task "\test" , instance "{abcdefgh-fab0-4a21-b51a-e25baaaa0000}" , action "C:\Windows\system32\cmd.EXE" with return code 1.</p>
  </blockquote>
  
  <p>The text in the <em>Last Run Result</em> column corresponds to this: <code>(0x1)</code>.</p>
  
  <p>However, when run on Windows Server 2012 (Azure VM), the following erroneous message appears:</p>
  
  <blockquote>
    <p>Task Scheduler successfully completed task "\test" , instance "{abcdefgh-fab0-4a21-b51a-e25bbbbb1111}" , action "C:\Windows\system32\cmd.EXE" <strong>with return code 0</strong>.</p>
  </blockquote>
  
  <p>(Emphasis mine.) However the text in the <em>Last Run Result</em> column reads: <code>Incorrect function: (0x80070001)</code>. This indicates that at least part of the Task Scheduling processor recognizes some degree of error condition. If I replace the arguments with <code>/c "exit /b 0"</code>, this column reads: <code>The operation completed successfully. (0x0)</code></p>
  
  <p>I've tried various combinations of values for user context, "start in" directory, arguments (with and without <code>/b</code>), and different operating systems under "Configure for:", to no avail.</p>
  
  <p><strong>Question:</strong> How can I get Task Scheduler on the Windows 2012 machine to display the correct return code in the history item?</p>
  
  <p><strong>Workaround:</strong> While I still want to understand this bizarre behavior (perhaps it indicates some more significant problem/misunderstanding), I am solving my original issue by adding the following to my task script:</p>
  
  <pre><code>$objMailMessage = New-Object System.Net.Mail.MailMessage
  $objMailMessage.From = "username@gmail.com"
  $objMailMessage.To.Add("recipient@example.com")
  $objMailMessage.Subject = "The task failed."
  #$objMailMessage.Body = "Detailed information."
  
  $smtp = new-object Net.Mail.SmtpClient("smtp.gmail.com", 587)
  $smtp.EnableSsl = $true
  $smtp.Credentials = new-object Net.NetworkCredential("username@gmail.com","pass")
  $smtp.send($objMailMessage)
  </code></pre>