Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

List Scheduled Tasks via script?

Status
Not open for further replies.

jasonp45

Programmer
Aug 23, 2001
212
0
0
US
I'm trying to list the names of the Scheduled Tasks on my 64-bit Win 2008 server so I can export them. I discovered that I can easily export them as XML files, all at once (in which case they can't be imported on another machine), or individually by name with the following command-line syntax:

Output all tasks:
Code:
schtasks /Query /XML >[desired output path & XML filename]

Output single task by name:
Code:
schtasks /Query /XML /TN [Task name] >[desired output path & XML filename, which should be synonymous with task name]

If I can build a list of task names I can then output each to XML by name; there doesn't seem to be a way to do this via the 'schtasks' command.

I tried the following WMI script but apparently it doesn't list jobs I've created via the Scheduled Task interface (either that or it doesn't work on 64-bit):

Code:
strComputer = "."
set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set colScheduledJobs = objWMI.ExecQuery("Select * from Win32_ScheduledJob")
for each objJob in colScheduledJobs
	MsgBox objJob.Name
next
 
I solved this by the way, in case anyone's interested:

Code:
Option Explicit

Dim oExpSchTsk 
Set oExpSchTsk = New cExport_Scheduled_Tasks
oExpSchTsk.Export_Scheduled_Tasks
Set oExpSchTsk = Nothing
WScript.Quit

Class cExport_Scheduled_Tasks

'<Initialization>
	Private Array_XML_Aggregate 'as String
	Private File_XML_Aggregate 'as String
	Private Path_Output_Folder 'as String
	Private Task_Author_String 'as String
	Private Text_XML_Aggregate 'as String
	Private oWiSH_Shell 'as Object: Windows Scripting Host Shell
	Private oFSO 'as Object: FileSystemObject
  
	Private Sub Class_Initialize()
		Set oFSO = CreateObject("Scripting.FileSystemObject")
		Set oWiSH_Shell = CreateObject("WScript.Shell")
		Path_Output_Folder = "C:\Scheduled.Tasks\Daily.Export\"
		File_XML_Aggregate = Path_Output_Folder & "Scheduled_Tasks_XML_Aggregate.xml"
		Task_Author_String = "<Author>[Domain\Username]]</Author>"
	End Sub  
'</Initialization>

'<Properties>
	Private Property Get Task_Is_Authored_By_User(ByVal iCounter) 'as Boolean
		Task_Is_Authored_By_User = False
		If InStr(Array_XML_Aggregate(iCounter), Task_Author_String) > 0 Then Task_Is_Authored_By_User = True
	End Property
'</Properties>	

'<Public Methods>
	Public Sub Export_Scheduled_Tasks()
		Output_Scheduled_Tasks_as_Aggregate_XML
		Read_Aggregate_XML_into_Memory
		Write_Tasks_as_Unique_XML_Files
	End Sub
'</Public Methods>

'<Primary Methods>
	Private Sub Output_Scheduled_Tasks_as_Aggregate_XML()
		Dim sCommandText 'as String
		If oFSO.FileExists(File_XML_Aggregate) Then oFSO.DeleteFile File_XML_Aggregate
		sCommandText = "Cmd.exe /C schtasks /Query /XML >" & File_XML_Aggregate
		Run_Command sCommandText
	End Sub
	
	Private Sub Read_Aggregate_XML_into_Memory()
		Dim oFile 'as Object = FileSystemObject.File
		Set oFile = oFSO.OpenTextFile(File_XML_Aggregate, 1) 'For Reading
		Text_XML_Aggregate = oFile.ReadAll
		oFile.Close
		Set oFile = Nothing
		Array_XML_Aggregate = Split(Text_XML_Aggregate, "<!-- \")
	End Sub
	
	Private Sub Write_Tasks_as_Unique_XML_Files()
		Dim iCounter 'as Integer
		Dim sFile_Output_Name 'as String
		Dim oFile 'as Object = FileSystemObject.File
		For iCounter = 1 to UBound(Array_XML_Aggregate) 'Skip first member as it contains only a header
			If Task_Is_Authored_By_User(iCounter) = True Then
				sFile_Output_Name = Path_Output_Folder & Left(Array_XML_Aggregate(iCounter), InStr(Array_XML_Aggregate(iCounter), " -->") - 1) & ".xml"
				Set oFile = oFSO.OpenTextFile(sFile_Output_Name, 2, True) 'ForWriting, Overwrite FileName if exists
				oFile.Write "<!-- \" & Array_XML_Aggregate(iCounter)
				oFile.Close
			End If
		Next
	End Sub
'</Primary Methods>	

'<Helper Methods>	
	Private Sub Run_Command(sCommandText)
		oWiSH_Shell.Run sCommandText, 7, True '7 = WindowStyle Inactive + Minimized; True = script execution halts until the program finishes
	End Sub
'</Helper Methods>

'<Termination>
	Private Sub Class_Terminate()
		oFSO.DeleteFile File_XML_Aggregate
		Set oFSO = Nothing
		Set oWiSH_Shell = Nothing
	End Sub  
 '</Termination>

End Class	

'<Notes>
	'XML files in folder are automatically overwritten by output files if synonymous by the FSO.OpenTextFile() options chosen
	'Modify Author Name and Output Folders as desired in Class_Initialize()
'</Notes>
 
Jason,

Thanks for posting your solution. I need to get this working in MS Access. I created a VBA code module and pasted in your code.

I'm getting a not defined error on:

New cExport_Scheduled_Tasks

Is there a reference that I would need to set?

Thanks!
Brooks
 
Another question ...

I'm running Win XP SP2 which doesn't support the /XML parameter, so I've been doing:

schtasks /query /v > schtasks.txt

The only problem is that the 'Task to Run' column gets truncated because of a long path and parameters. Any idea how to get around this? I thought XML might be the answer, but no go with XP.

I've found other code samples on the web that will enumerate tasks but only if they were entered by the AT command.
 
I recently used this command also... I had to use the "/fo CSV" switch to get usable output. I then had to write a parser to extract the fields that I was interested in.

I can clean it up and post, if you are interested.

PSC

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top