HowTo: Enumerate Scheduled Tasks (Using SCHTASKS.EXE)
I cooked up a script to query the scheduled jobs on remote servers and pipe the output to a CSV file and utilize ADODB to query, populate an HTML template and use mailsend.exe to attach the HTML and send an email notification.
Below are the common parameters using SCHTASKS /Query to enumerate the scheduled jobs which will be the back bone of the script.
SCHTASKS /Query [/S system [/U username [/P password]]] [/FO format]
[/NH] [/V] [/?]
Description:
Enables an administrator to display the scheduled tasks on the
local or remote system.
Parameter List:
/S system - Specifies the remote system to connect to.
/U username - Specifies the user context under which the command should execute.
/P password - Specifies the password for the user context.
/FO format - Specifies the output format to be displayed. Valid values: TABLE, LIST, CSV.
/NH - Specifies that the column header should not be displayed in the output.
Valid only for TABLE and CSV formats.
/V - Specifies additional output to be displayed.
/? - Displays this help/usage.
Examples:
SCHTASKS /Query
SCHTASKS /Query /?
SCHTASKS /Query /S system /U user /P password
SCHTASKS /Query /FO LIST /V /S system /U user /P password
SCHTASKS /Query /FO TABLE /NH /V
Actual SCHTASKS command line executed from the script:
Schtasks.exe /QUERY /S %SERVERNAME% /FO CSV /NH /V > OutputFile.CSV
Actual Mailsend command line executed from the script:
Mailsend.exe -sub %Subject% -v < %Template% -cs "ISO-8859-1" -a "%Attachment%,text/html,i"
-sub "Mail Subject"
-v Verbose Mode
< %Template%
Template File Contains the following:
1st line : SMTP Host
2nd line: Domain
3rd line: Sender's Email Address
4th Line: Recipient's Email Address
-cs character set - for text/plain attachments (default is us-ascii)
-a file,mime_type - attach this file
CODE SNIPPET:
Function GetTasks()
Set oShell = WScript.CreateObject ("WSCript.shell")
Set objShell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
workfile = "OutputFile.csv"
oShell.run "cmd /c schtasks /QUERY /S %SERVERNAME% /FO CSV /NH /V > "& workfile,0,true
Set objConnection = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")
strPathtoTextFile = "%PATHtoCSV%"
objConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & strPathtoTextFile & ";" & _
"Extended Properties=""text;HDR=YES;FMT=Delimited"""
strFile = "OutputFile.csv"
objRecordset.Open "Select * FROM " & strFile , _
objConnection, adOpenStatic, adLockOptimistic, adCmdText
Do Until objRecordset.EOF
'If the Last Result of the Job <> 0 then get the job details and write it to the HTML Template
If objRecordset.Fields.Item("Last Result") <> 0 and objRecordset.Fields.Item("Next Run Time") <> "Disabled" and objRecordset.Fields.Item("TaskName") <> "cluster_info" Then
WSCript.Echo objRecordset.Fields.Item("HostName") & " | " & _
objRecordset.Fields.Item("TaskName") & " | " & _
objRecordset.Fields.Item("Last Result") & " | " & _
objRecordset.Fields.Item("Next Run Time") & " | " & _
objRecordset.Fields.Item("Last Run Time") & " | " & _
objRecordset.Fields.Item("Run As User") & " | " & _
objRecordset.Fields.Item("Schedule") & " | " & _
objRecordset.Fields.Item("Task To Run")
CreateMail objRecordset.Fields.Item("HostName"), objRecordset.Fields.Item("TaskName"), objRecordset.Fields.Item("Last Result"), objRecordset.Fields.Item("Last Run Time"), objRecordset.Fields.Item("Next Run Time"), objRecordset.Fields.Item("Task To Run"), objRecordset.Fields.Item("Run As User"), objRecordset.Fields.Item("Schedule"), mailfile
End If
objRecordset.MoveNext
Loop
oShell.Run "cmd /c DEL /Q " & workfile,0
Set oShell = Nothing
End Function