Windows Task Scheduler reports incorrect/inconsistent result code


<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" rel="nofollow">this answer</a> to trigger an email.

<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.

<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: cmd</li> <li>Arguments: /c "exit /b 1"</li> <li>Start in: blank</li> </ul></li> <li>Everything else: default</li> </ul>

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":


Task Scheduler successfully completed task "\test" , instance "{abcdefgh-fab0-4a21-b51a-e25baaaa0000}" , action "C:\Windows\system32\cmd.EXE" with return code 1.


The text in the <em>Last Run Result</em> column corresponds to this: (0x1).

However, when run on Windows Server 2012 (Azure VM), the following erroneous message appears:


Task Scheduler successfully completed task "\test" , instance "{abcdefgh-fab0-4a21-b51a-e25bbbbb1111}" , action "C:\Windows\system32\cmd.EXE" <strong>with return code 0</strong>.


(Emphasis mine.) However the text in the <em>Last Run Result</em> column reads: Incorrect function: (0x80070001). This indicates that at least part of the Task Scheduling processor recognizes some degree of error condition. If I replace the arguments with /c "exit /b 0", this column reads: The operation completed successfully. (0x0)

I've tried various combinations of values for user context, "start in" directory, arguments (with and without /b), and different operating systems under "Configure for:", to no avail.

<strong>Question:</strong> How can I get Task Scheduler on the Windows 2012 machine to display the correct return code in the history item?

<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:

$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)


I'm seeing a similar issue on Server 2012 with a batch file that looks like it succeeds, shows a return value of 0 in event log, but a Last Run Result of 0x80070001.

I see MSFT has a hotfix available for Server 2012 which might address this issue:

<a href="http://support.microsoft.com/kb/3003689" rel="nofollow">http://support.microsoft.com/kb/3003689</a>


  • Voicemail detection on FreeSWITCH
  • GWT Hello World ; java.lang.IllegalArgumentException: No enum constant com.google.gwt.dev.cfg.Module
  • Looping through a case statement in Ruby?
  • How to get self-referenced table tree structure, based on any node in tree?
  • VBS Stop option forced to kill on 1 sec :(
  • Adding a QSizeGrip to the corner of a QLabel
  • What is the absolute fastest way to implement a concurrent queue with ONLY one consumer and one prod
  • How to make exact CSS3 Linear gradient like it's in the image?
  • Solving System of Second Order Ordinary Differential Equation in Matlab
  • Can I execute MySQL SQL statements in FireFox?
  • Transforming a Cassandra OrderedMapSerializedKey to a Python dictionary
  • Not a valid Win32 application… Python, PyInstaller, Windows7
  • WPToolkit not appear in the references after successful install
  • Postgres JSONB: where clause for arrays of arrays
  • Spring Security Config Error while server startup
  • DES3 encryption: ruby openssl::cipher vs. oracle dbms_obfuscation_toolkit
  • How to make gcc or ld report undefined symbols but not fail?
  • Is it safe to uninstall VS Express after installing VS Ultimate?
  • Scrolling News Ticker Jquery - Issues
  • How to capture enclosing scope in Runnable
  • How to replace TouchesBegan with UIGestureRecognizer
  • Titanium doesn't recognize Android SDK on Windows
  • Using extern @class in order to add a category?
  • “A GKScore must specify a leaderboard.”
  • Asynchronous Sockets - Handling false socket.AcceptAsync values
  • Find longest path less than or equal to given value of an acyclic, directed graph in Python
  • several dataProvider per one Test in TestNG
  • Invalid object name 'dbo.Item'
  • Jenkins: FATAL: Could not initialize class hudson.util.ProcessTree$UnixReflection
  • TextToSpeech.setEngineByPackageName() triggers NullPointerException
  • Not able to aggregate on nested fields in elasticsearch
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • Highlight one bar in a series in highcharts?
  • Apache 2.4 and php-fpm does not trigger apache http basic auth for php pages
  • Incrementing object id automatically JS constructor (static method and variable)
  • Calling of Constructors in a Java
  • PHP: When would you need the self:: keyword?
  • KeystoneJS: Relationships in Admin UI not updating
  • Free memory of cv::Mat loaded using FileStorage API
  • unknown Exception android