Created
June 9, 2011 15:02
-
-
Save exhuma/1016899 to your computer and use it in GitHub Desktop.
Query Windows Scheduled Tasks and do something with it
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment