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 »

200gc Game 12/200

April 17th, 2011 by exhuma.twn

Matchup: PvZ (Replay)
Map: Metalopolis
Outcome: Win
Worth watching: Properly scouted and punished a 6-pool.

Properly scouted and recognized a 6-pool by realizing there were not many drones on the enemy’s minerals. While the wall-in was a bit slow in the beginning due to scouting micro, it turned out well. I have the feeling that the enemy’s cheese was poorly executed. I expected an earlier attack. I decided to focus on Zealots and push early, which was the right thing to do. I may have overdone it a bit by building two Assimilators in case I needed to transition. Nevertheless. It was OK.

Posted in wickedexhuma.255 | No Comments »

200gc Game 11/200

March 18th, 2011 by exhuma.twn

Matchup: PvP (Replay)
Map: Xel’Naga Caverns
Outcome: Win
Worth watching: Nope.

I learned from Game 8. I positioned my attacking force better on this map. Good good! However, I was again not aggressive enough. I could have ended this earlier.

Posted in wickedexhuma.255 | No Comments »

200gc Game 10/200

March 18th, 2011 by exhuma.twn

Matchup: PvZ (Replay)
Map: Backwater Gulch
Outcome: Win
Worth watching: Nah. Very late pool by the zerg, which made my game easy.

I accidentally ran for a long time on only two gateways. This slowed my Stalker production considerably.

Recap after 10 Games

Bronze @ 778 points (+121)
W/L ratio: 54% (+1%)
Ranked #16

Posted in wickedexhuma.255 | No Comments »

200gc Game 9/200

March 18th, 2011 by exhuma.twn

Matchup: Pvt (Replay)
Map: Shattered Temple
Outcome: Win
Worth watching: Maybe (My first offensive Force Field at 9:10)

I did a better job at droning. But I should have played more aggressively. The game could have ended earlier. This is again due to bad scouting and I could have run an observer around his back. I did not know the size of the enemies army so I stood cautiously outside his base until I saw an opening. The Cannons were annoying. Blink would have been useful.

Posted in wickedexhuma.255 | No Comments »

200gc Game 8/200

March 18th, 2011 by exhuma.twn

Matchup: PvZ (Replay)
Map: Xel’Naga Caverns
Outcome: Win
Worth watching: not really

Pretty easy game. Steamrolled the Zerg. I could have repositioned my Stalkers earlier to cut losses. I inadvertently positioned them between both hatches. I’ll have to remember that. In other games this could mean life or death, as the Zealots could not tank properly.

Posted in wickedexhuma.255 | No Comments »

200gc Game 7/200

March 12th, 2011 by exhuma.twn

Matchup: Pvt (Replay)
Map: Delta Quadrant
Outcome: Loss
Worth watching: hell no

As said in the two earlier posts. I was/am tired. I should not even have started this game. There is nothing more to say.

Posted in wickedexhuma.255 | No Comments »

200gc Game 6/200

March 12th, 2011 by exhuma.twn

Matchup: PvP (Replay)
Map: Typhon Peaks
Outcome: Loss
Worth watching: maybe

The enemy went 4gate. I managed to deceive him though with my scouting probe. So he pushed towards the wrong base. Still. I should have been able to hold that off.
Around the 12 min mark, the enemy went DTs. I was lucky to have an obs in my stalker ball. So I could deal with it. It looked good until I totally missed that I had been attacked again in my main. I lost everything. I was about to quit, but decided to give it a try nevertheless. I managed to get back on my feet. Still. I was way behind in my economy. I did not stand a chance.

Again. I felt tired the entire game. I really should take this into account when playing… :(

Posted in wickedexhuma.255 | No Comments »

200gc Game 5/200

March 12th, 2011 by exhuma.twn

Matchup: PvT (Replay)
Map: Slag Pits
Outcome: Win
Worth watching: no

Pretty boring and standard game. Stalkers + Colossi = Win. Some Banshee harassment from the Terran, but that was to be expected. Mediocre macro from me though. I could have done better. Guess I’m tired.

Posted in wickedexhuma.255 | No Comments »

« Previous Entries

Pages

Recent Posts

Categories

Links


Archives

Meta