PGTS PGTS Pty. Ltd.   ACN: 007 008 568

point Site Navigation

point Other Blog Threads

  Valid HTML 4.01 Transitional

   Stop The Internet Filter!

   No Clean Feed

   The Internet Filter Is An Ex-parrot!

PGTS Humble Blog

Thread: Tips/Tricks For Programming etc

Author Image Gerry Patterson. The world's most humble blogger
Opinions are like arseholes. Everyone has one -- Dirty Harry

The New Improved Task Scheduler

Chronogical Blog Entries:

Date: Sat, 31 Mar 2018 23:00:00 +1100

Although they've never admitted it. There were a lot of problems with earlier versions of the Windows task scheduler. And there still are quite a few. One of the things I found difficult was working out what exactly went wrong, when a scheduled task failed.

When a scheduled task fails because of some problem with the scheduling process, it returns a code. I found a list online, which was in Hex. However the task scheduler command line reports the errors as (32 bit) decimal ... And will faithfully convert numbers larger than (unsigned) 0x80000000 into negative numbers using standard 2's compliment arithmetic. So you'll need to get busy with your Hex calculator to work out what the error was.

In my case, in order to cope with this, I wrote the script below (derived from the hex codes that Microsoft supplied). This would allow me to quickly convert a status code into a meaningful message. It runs quite well in Cygwin, but should in theory run in any modern perl distribution such as Activestate, Strawberry etc.

One of the error codes that at first nearly drove me to distraction was the return code of 0x1 (1). This means that the CMD shell has failed ... But, alas, there was no actual information about the error. After many hours of painful trial and error, I determined that the easiest way to diagnose these faults was to try the following:

  1. Run the task with the "schtasks -run" command. If it fails, the fault is not related to the schedule.
  2. Run the command from the command line. If it fails you should see the error message (unless you are redirecting stderr).
  3. If the command succeeds when running from the command line ... And this is the part that almost had me tearing my hair out ... The most likely cause is that somewhere in your code you are using a drive mapped to an SMB share. This will usually fail in the scheduler. Either place appropriate "net use" commands that map shares at the start of your script (if it is a CMD script), or replace mapped paths with Windows UNC paths.

Perl script for all error codes follows:

use strict;
use warnings;

=head1 Source

 This table was obtained from:

 The output was put into ms_error.txt and converted into
 hash lookups with extr_ms_error


# CAUTION This table contains TAB characters, make sure you get the TABS if you copy this script ...

my %Msg = (

	267008		=> "SCHED_S_TASK_READY	The task is ready to run at its next scheduled time.",
	267009		=> "SCHED_S_TASK_RUNNING	The task is currently running.",
	267010		=> "SCHED_S_TASK_DISABLED	The task will not run at the scheduled times because it has been disabled.",
	267011		=> "SCHED_S_TASK_HAS_NOT_RUN	The task has not yet run.",
	267012		=> "SCHED_S_TASK_NO_MORE_RUNS	There are no more runs scheduled for this task.",
	267013		=> "SCHED_S_TASK_NOT_SCHEDULED	One or more of the properties that are needed to run this task on a schedule have not been set.",
	267014		=> "SCHED_S_TASK_TERMINATED	The last run of the task was terminated by the user.",
	267015		=> "SCHED_S_TASK_NO_VALID_TRIGGERS	Either the task has no triggers or the existing triggers are disabled or not set.",
	267016		=> "SCHED_S_EVENT_TRIGGER	Event triggers do not have set run times.",
	267035		=> "SCHED_S_SOME_TRIGGERS_FAILED	The task is registered, but not all specified triggers will start the task.",
	267036		=> "SCHED_S_BATCH_LOGON_PROBLEM	The task is registered, but may fail to start. Batch logon privilege needs to be enabled for the task principal.",
	267045		=> "SCHED_S_TASK_QUEUED	The Task Scheduler service has asked the task to run.",

	# These are all reported as negative numbers by "schtasks"
	2147750665	=> "SCHED_E_TRIGGER_NOT_FOUND	A task's trigger is not found.",
	2147750666	=> "SCHED_E_TASK_NOT_READY	One or more of the properties required to run this task have not been set.",
	2147750667	=> "SCHED_E_TASK_NOT_RUNNING	There is no running instance of the task.",
	2147750668	=> "SCHED_E_SERVICE_NOT_INSTALLED	The Task Scheduler service is not installed on this computer.",
	2147750669	=> "SCHED_E_CANNOT_OPEN_TASK	The task object could not be opened.",
	2147750670	=> "SCHED_E_INVALID_TASK	The object is either an invalid task object or is not a task object.",
	2147750671	=> "SCHED_E_ACCOUNT_INFORMATION_NOT_SET	No account information could be found in the Task Scheduler security database for the task indicated.",
	2147750672	=> "SCHED_E_ACCOUNT_NAME_NOT_FOUND	Unable to establish existence of the account specified.",
	2147750673	=> "SCHED_E_ACCOUNT_DBASE_CORRUPT	Corruption was detected in the Task Scheduler security database; the database has been reset.",
	2147750674	=> "SCHED_E_NO_SECURITY_SERVICES	Task Scheduler security services are available only on Windows NT.",
	2147750675	=> "SCHED_E_UNKNOWN_OBJECT_VERSION	The task object version is either unsupported or invalid.",
	2147750676	=> "SCHED_E_UNSUPPORTED_ACCOUNT_OPTION	The task has been configured with an unsupported combination of account settings and run time options.",
	2147750677	=> "SCHED_E_SERVICE_NOT_RUNNING	The Task Scheduler Service is not running.",
	2147750678	=> "SCHED_E_UNEXPECTEDNODE	The task XML contains an unexpected node.",
	2147750679	=> "SCHED_E_NAMESPACE	The task XML contains an element or attribute from an unexpected namespace.",
	2147750680	=> "SCHED_E_INVALIDVALUE	The task XML contains a value which is incorrectly formatted or out of range.",
	2147750681	=> "SCHED_E_MISSINGNODE	The task XML is missing a required element or attribute.",
	2147750682	=> "SCHED_E_MALFORMEDXML	The task XML is malformed.",
	2147750685	=> "SCHED_E_TOO_MANY_NODES	The task XML contains too many nodes of the same type.",
	2147750686	=> "SCHED_E_PAST_END_BOUNDARY	The task cannot be started after the trigger end boundary.",
	2147750687	=> "SCHED_E_ALREADY_RUNNING	An instance of this task is already running.",
	2147750688	=> "SCHED_E_USER_NOT_LOGGED_ON	The task will not run because the user is not logged on.",
	2147750689	=> "SCHED_E_INVALID_TASK_HASH	The task image is corrupt or has been tampered with.",
	2147750690	=> "SCHED_E_SERVICE_NOT_AVAILABLE	The Task Scheduler service is not available.",
	2147750691	=> "SCHED_E_SERVICE_TOO_BUSY	The Task Scheduler service is too busy to handle your request. Please try again later.",
	2147750692	=> "SCHED_E_TASK_ATTEMPTED	The Task Scheduler service attempted to run the task, but the task did not run due to one of the constraints in the task definition.",
	2147750694	=> "SCHED_E_TASK_DISABLED	The task is disabled.",
	2147750695	=> "SCHED_E_TASK_NOT_V1_COMPAT	The task has properties that are not compatible with earlier versions of Windows.",
	2147750696	=> "SCHED_E_START_ON_DEMAND	The task settings do not allow the task to start on demand.",
	2147943726	=> "LOGON_FAILURE	Unknown user name or bad password.",


my ($status) = @ARGV;
unless ($status && ($status =~ /^\d+$/ || $status =~ /^\-\d+$/)) {
	print "\nUsage: $0 STATUS\n\nWhere STATUS is one of the numbers below:\n\n";
	print "     STATUS LABEL                               Description\n";
	print "     ------ ----------------------------------- ------------------------------\n";
	foreach my $n (sort {$a <=> $b} keys %Msg) {
		printf ("%11s %-35.35s %s\n",$n,split(/\t/,$Msg{$n}));
# If the number is negative add 4294967296 (must be greater than -2147483648)
$status += 4294967296 if ($status < 0 && $status > -2147483648);
if ($Msg{$status}) {
	print "$Msg{$status}\n";
} else {
	die "Do not have details for status: $status\n";

Other Blog Posts In This Thread:

Copyright     2018, Gerry Patterson. All Rights Reserved.