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 »

Windows script to remove old files

June 8th, 2011 by exhuma.twn

Simple script… still, I thought I’d share…

/**
* Remove all files and folders that are older than a set number of days.
*
* @param rootURI The URI of the root folder. All old files and folders in this
* folder are removed.
* @param days Files older than this number of days are deleted
*/
function purgeFiles(rootURI, days) {

  var fso = new ActiveXObject("Scripting.FileSystemObject");
  var rootFolder = fso.GetFolder(rootURI);
  var subFolders = new Enumerator(rootFolder.SubFolders);

  // create a date before which files are considered "old"
  var threshold_date = new Date();
  threshold_date.setDate(threshold_date.getDate()-days);

  // loop over each file
  subFolders.moveFirst();
  for(;!subFolders.atEnd(); subFolders.moveNext()){
     var f = subFolders.item();
     if(f.DateCreated < threshold_date ){
        //WScript.Echo("Deleting "+f.Name+" last accessed on: "+f.DateCreated);
        f.Delete(true);
     }
  }

}

purgeFiles("C:/path/to/folder", 300);

view raw purgeFiles.js This Gist brought to you by GitHub.

Posted in Coding Voodoo | No Comments »

Pages

Recent Posts

Categories

Links


Archives

Meta