When working with scheduled tasks on a windows machine, I often wished I could work on the command line with the scheduler. As we all know, a command line interface gives us the ability to script, preventing manual repeating work.

Unfortunately, until a few days ago I did not find a solution for this, but after another extensive search I found schtasks.exe. It turned out this tool was already available from Windows server 2003.

The command line tool gives the ability to do everything you can do in the Windows scheduler, aka “Process the content of the Windows scheduler”. As common, using the /? parameter gives the basic options which are:

•  /Run
•  /End
•  /Create
•  /Delete
•  /Query
•  /Change
•  /ShowSid

The first purpose I used it for was creating range of backup jobs which had to be executed during the night, and deleted afterwards (one-time only):

SCHTASKS /Create /RU AMIS\<username> /RP "my passw" /SC ONCE  /TN "Name of scheduled task" /TR "c:\path\to\my\command.cmd" /ST HH:MM /SD MM/DD/YYYY /Z

Let’s have a look at the parameters used in this case:

•  /Create The obvious: create a new scheduled task
•  /RU The user as which the task should be executed
•  /RP The password for the user executing the task
•  /SC The schedule this task will follow, in this case once
•  /TN The name of the task that will appear in the task scheduler, use quotes if you require spaces
•  /TR The full path and name to the file to be executed
•  /ST  The start time of the task
•  /SD The start date of the task
•  /Z  Causes the task to be deleted when finished

Of course a lot more options are available. Use the /Create /? parameters for an overview.

Next step is to see which tasks are scheduled, and what their status is.

First, execute schtasks /query /? to see the possible options. Since we like scripting and find only the relevant data, I used the following command to get a full listing in a .csv file, waiting to be processed.

SCHTASKS /FO CSV /V > scheduler.csv

In this specific case I used perl to analyze the produced file and generate a small report with only the lines I’m interested in (failed, disabled, errors etc.). The report could be send using blat or similar.
The most important part the perl script:

open FILE, "$scheduler" or die$!;
while () {
my $line =$_;
my @fields = split(/\",\"/,$line); if ($fields[1] =~ /Disabled/ || $fields[2] =~ /"Could not start"/ ||$fields[2] =~ /Running/) {
$report_line .=$fields[1]. "\t".$fields[2]."\t".$fields[3]."\n";;
}
}
close FILE or die;


1. First open the file for processing line by line.
2. Next step is to split the content of the line to @fields, using “,” as separator (including the double “). Only the comma cannot be used because is also found in the timestamp next run, and could be in task description also.
3. Define the fields which are of interest, and test if any value causes it to be reported. If so, add the required fields to variable $report_line. 4. When done processing, write the content of$report_line to the output file.

Using an administrator account one will also be able to execute this remotely on another machine, i.e. from your admin server to all other Windows servers in your farm.

Of course a lot more can be done using this small utility. Explore the help or spend some coins in your favourite search engine.

Happy scripting,

Jeroen

### About Author

Jeroen is Oracle consultant at AMIS

Comments are closed.