JScript to query scheduled tasks

June 9th, 2011 by exhuma.twn

Soon, we will need to send out notifications as soon something bad happens with a scheduled task on Windows. The following JScript file runs natively on Windows and is capable of just that. It uses the command line tool “schtasks” to query the information and wraps the result into a list of usable object instances.

It’s possible to use this list to react to important events in the job executions. For example, you could loop through the list and send emails to the appropriate people if the variable “lastResult” is non-zero.

/**
* Naive CSV splitter
*
* This splitter is *very* simplistic and may result in errors when parsing
* unknown CSV sources. This works well in the current problem domain.
*
* As we have well defined data, with no escaped quotes inside the fields, we
* can sefaly assume that this will work.
*/
function simpleCsvSplit(lineText){
var columns = [];
var inside_quote = false;
var current_data = "";
var i=0;
for(i=0; i<lineText.length; i++){
var chr = lineText.substring(i, i+1);
if (chr === "\""){
inside_quote = !inside_quote;
continue;
} else if (chr === "," && !inside_quote){
columns[columns.length] = current_data;
current_data = "";
}
if (inside_quote){
current_data += chr;
}
}
columns[columns.length] = current_data;
return columns;
}

/**
* A container object for the task metadata
* This makes further processing with the data a lot more expressive.
*
* @param lineText A string taken from the CSV output representing one line
*/
var Task = function(lineText){
// parse the CSV data
columns = simpleCsvSplit(lineText);

// put everything into explicitly named variables
this.hostName = columns[0];
this.name = columns[1];
this.nextRun = columns[2];
this.status = columns[3];
this.lastRun = columns[4];
this.lastResult = columns[5];
this.creator = columns[6];
this.schedule = columns[7];
this.command = columns[8];
this.startIn = columns[9];
this.comment = columns[10];
this.scheduledState = columns[11];
this.scheduledType = columns[12];
this.startTime = columns[13];
this.startDate = columns[14];
this.endDate = columns[15];
this.days = columns[16];
this.months = columns[17];
this.runAs = columns[18];
this.deleteIfNotRescheduled = columns[19];
this.stopIf = columns[20];
this.repeatEvery = columns[21];
this.repeatUntilTime = columns[22];
this.repeatUntilDuration = columns[23];
this.repeatStopIfRunning =columns[24];
this.idleTime = columns[25];
this.powerManagement = columns[26];
};

// execute the shell command to retrieve the scheduled tasks
var WshShell = new ActiveXObject("WScript.Shell");
var oExec = WshShell.Exec("schtasks /Query /FO CSV /V");

// retrieve the lines from stdout and save them as tasks
var tasks = [];
while( !oExec.StdOut.AtEndOfStream){
   tasks[tasks.length] = oExec.StdOut.ReadLine();
}

// now do something with the tasks
for(var i=2; i<tasks.length; i++){
var tmp = new Task(tasks[i]);
WScript.Echo(tmp.lastResult + " - " + tmp.status);
}

Posted in Coding Voodoo | No Comments »

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

Pages

Recent Posts

Categories

Links


Archives

Meta