<

[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x

VBScript and Task Scheduler 2.0 : Listing Scheduled Tasks

Published on
17,766 Points
11,666 Views
1 Endorsement
Last Modified:
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come across something I have not yet done.  Recently I needed to work with Task Scheduler 2.0 using VB Script.  I found that sample scripts were in short supply with most not having enough of the objects in them.  Luckily the Documentation is decent, but I still wanted a sample script.  Therefore I built a script for myself that would list all scheduled tasks and their properties.  I wanted to share for anyone else who was looking.

Task Scheduler 2.0 was originally released with the Vista operating system and should be the object used when looking at scheduled tasks in the newer Microsoft Operating Systems.  It will give you access to some of the many new options introduced in that version.  Since Task Scheduler 2.0 is not available on older operating systems, just be aware that this code will not work on those systems.

The script below will list all created tasks with their options and save it to a text file of “C:\TaskListing.txt.”  This file name and/or path can be updated at the beginning of the script (Line 7).    I have added a comment in each section with a link to the relevant documentation for that section.  The code has all the possible properties and conversions so that you can see all the options.  You should be able to easily pull out the pieces you need if you only want to look at certain types of scheduled tasks.  If you are unsure about any values, please look for the commented link for that section to see the official documentation for that object.

I have tested with several different types of scheduled tasks, but did not test every scenario.    If you find something that is wrong, please let me know.

Happy Coding!

The full Code:
' ----------------------------------------------------------------------
' Script to list all Scheduled tasks and their available properties
'	Written for TaskSchedule 2.0
' ----------------------------------------------------------------------

' <<** Set Log File name and path here **>>
CONST LOGFILE = "C:\TaskListing.txt"

'http://msdn.microsoft.com/en-us/library/windows/desktop/aa382079%28v=vs.85%29.aspx

' Option Explicit
Dim objFSO, objTextFile
Dim objTaskService, objTaskFolder, colTasks
Dim objTask, strstrTaskState
Dim objTaskDefinition, colActions, objTaskAction, arrAttachments, strAttachment
Dim colHeaderFields, objHeaderPair
Dim objPrincipal, objRegistrationInfo, objTaskSettings, objIdleSettings, objTaskNetworkSettings
Dim colTaskTriggers, objTaskTrigger, objTaskRepitition

' Write Task Data to a File 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.CreateTextFile(LOGFILE)

' Create the TaskobjTaskService object and connect
Set objTaskService = CreateObject("Schedule.Service")
call objTaskService.Connect()

' Get the task folder that contains the tasks. 
Set objTaskFolder = objTaskService.GetFolder("\")
 
' Get all of the tasks (Enumeration of 0 Shows all including Hidden.  1 will not show hidden)
Set colTasks = objTaskFolder.GetTasks(0)

If colTasks.Count = 0 Then 
    objTextFile.WriteLine "No tasks are registered."
Else
    For Each objTask In colTasks
		' http://msdn.microsoft.com/en-us/library/windows/desktop/aa382079%28v=vs.85%29.aspx
		With objTask
			objTextFile.WriteLine "Task Name: " & .Name 
			objTextFile.WriteLine " -Enabled:" & .Enabled
			objTextFile.WriteLine " -LastRunTime:" & .LastRunTime
			objTextFile.WriteLine " -LastTaskResult:" & .LastTaskResult
			objTextFile.WriteLine " -NextRunTime:" & .NextRunTime
			objTextFile.WriteLine " -NumberOfMissedRuns:" & .NumberOfMissedRuns
			objTextFile.WriteLine " -Path:" & .Path
			Select Case .State 
				Case "0"
					strTaskState = "Unknown"
				Case "1"
					strTaskState = "Disabled"
				Case "2"
					strTaskState = "Queued"
				Case "3"
					strTaskState = "Ready"
				Case "4"
					strTaskState = "Running"
			End Select
			objTextFile.WriteLine " -Task State: " & .State & "=" & strTaskState
			
			' http://msdn.microsoft.com/en-us/library/windows/desktop/aa382542%28v=vs.85%29.aspx
			Set objTaskDefinition = .Definition
			With objTaskDefinition
				objTextFile.WriteLine " -TASK DEFINITION"
				objTextFile.WriteLine "   -Data:" & .Data
				'http://msdn.microsoft.com/en-us/library/windows/desktop/aa446803%28v=vs.85%29.aspx
				Set colActions = objTaskDefinition.Actions
				For each objTaskAction in colActions
					With objTaskAction
						objTextFile.WriteLine "   -ACTIONS"
						objTextFile.WriteLine "     -ID:" & .Id
						Select Case .Type
							Case 0
								objTextFile.WriteLine "     -Type:" & .Type & "= Execute / Command Line Operation"
								objTextFile.WriteLine "     -Arguments :" & .Arguments  
								objTextFile.WriteLine "     -Path:" & .Path
								objTextFile.WriteLine "     -WorkingDirectory:" & .WorkingDirectory
							Case 5
								objTextFile.WriteLine "     -Type:" & .Type & "= Handler"
								objTextFile.WriteLine "     -ClassId:" & .ClassId 
								objTextFile.WriteLine "     -Data:" & .Data 
							Case 6
								objTextFile.WriteLine "     -Type:" & .Type & "= Email Message"
								objTextFile.WriteLine "     -From:" & .From
								objTextFile.WriteLine "     -ReplyTo:" & .ReplyTo 
								objTextFile.WriteLine "     -To:" & .To 
								objTextFile.WriteLine "     -Cc:" & .Cc
								objTextFile.WriteLine "     -Bcc:" & .Bcc 
								objTextFile.WriteLine "     -Subject:" & .Subject
								objTextFile.WriteLine "     -Body:" & .Body
								objTextFile.WriteLine "     -Server:" & .Server
								arrAttachments = .Attachments
								For each strAttachment in arrAttachments 
									objTextFile.WriteLine "     -Attachment:" & strAttachment
								Next 'strAttachment
								Set colHeaderFields = .HeaderFields
								For each objHeaderPair in ColHeaderFields
									objTextFile.WriteLine "     -HeaderName:" & objHeaderPair.Name  & " | Value:" & objHeaderPair.Value
								Next 'objHeaderPair
							Case 7
								objTextFile.WriteLine "     -Type:" & .Type & "=Message Box"
								objTextFile.WriteLine "     -Title:" & .Title
								objTextFile.WriteLine "     -MessageBody:" & .MessageBody
						End Select
					End With 'objTaskAction
				Next ' objTaskAction
				
				' http://msdn.microsoft.com/en-us/library/windows/desktop/aa382071%28v=vs.85%29.aspx
				Set objPrincipal = .Principal
				objTextFile.WriteLine "   -PRINCIPAL"
				With objPrincipal
					objTextFile.WriteLine "     -ID:" & .Id
					objTextFile.WriteLine "     -DisplayName:" & .DisplayName
					objTextFile.WriteLine "     -GroupId:" & .GroupId
					objTextFile.WriteLine "     -ID:" & .Id
					Select Case .LogonType
						Case 0
							objTextFile.WriteLine "     -LogonType:" & .LogonType & " = None"
						Case 1
							objTextFile.WriteLine "     -LogonType:" & .LogonType & " = Password"
						Case 2
							objTextFile.WriteLine "     -LogonType:" & .LogonType & " = Service 4 Users"
						Case 3
							objTextFile.WriteLine "     -LogonType:" & .LogonType & " = Interactive (User Must be logged in)"
						Case 4
							objTextFile.WriteLine "     -LogonType:" & .LogonType & " = Group"
						Case 5
							objTextFile.WriteLine "     -LogonType:" & .LogonType & " = Local Service/System or Network Service"
						Case 6
							objTextFile.WriteLine "     -LogonType:" & .LogonType & " = Interactive Token then Try Password"
					End Select
					Select Case .RunLevel
						Case 0 
							objTextFile.WriteLine "     -RunLevel:" & .RunLevel & " = Least Privileges (LUA)"
						Case 1 
							objTextFile.WriteLine "     -RunLevel:" & .RunLevel & " = Highest Privileges"
					End Select
							
					objTextFile.WriteLine "     -UserId:" & .UserId
				End With 'objPrincipal
				
				'http://msdn.microsoft.com/en-us/library/windows/desktop/aa382100%28v=vs.85%29.aspx
				Set objRegistrationInfo = .RegistrationInfo
				objTextFile.WriteLine "   -REGISTRATION INFO"
				With objRegistrationInfo
					objTextFile.WriteLine "     -Author:" & .Author
					objTextFile.WriteLine "     -Date:" & .Date
					objTextFile.WriteLine "     -Description:" & .Description
					objTextFile.WriteLine "     -Date:" & .Date
					objTextFile.WriteLine "     -Documentation :" & .Documentation 
					objTextFile.WriteLine "     -SecurityDescriptor:" & .SecurityDescriptor 
					objTextFile.WriteLine "     -Source:" & .Source
					objTextFile.WriteLine "     -URI:" & .URI
					objTextFile.WriteLine "     -Version:" & .Version
				End With 'objRegistrationInfo
				
				'http://msdn.microsoft.com/en-us/library/windows/desktop/aa383480%28v=vs.85%29.aspx
				Set objTaskSettings = .Settings
				objTextFile.WriteLine "   -TASK SETTINGS"
				With objTaskSettings
					objTextFile.WriteLine "     -AllowDemandStart:" & .AllowDemandStart
					objTextFile.WriteLine "     -AllowHardTerminate:" & .AllowHardTerminate
					Select Case .Compatibility
						Case 0
							objTextFile.WriteLine "     -Compatibility:" & .Compatibility & " = compatible with the AT command"
						Case 1
							objTextFile.WriteLine "     -Compatibility:" & .Compatibility & " = compatible with Task Scheduler 1.0"
						Case 2
							objTextFile.WriteLine "     -Compatibility:" & .Compatibility & " = compatible with Task Scheduler 2.0 (Windows Vista / Windows 2008"
						Case 3 ' Not Documented
							objTextFile.WriteLine "     -Compatibility:" & .Compatibility & " = compatible with Task Scheduler 2.0 (Windows 7 / Windows 2008 R2"
					End Select
					objTextFile.WriteLine "     -DeleteExpiredTaskAfter:" & .DeleteExpiredTaskAfter
					objTextFile.WriteLine "     -DisallowStartIfOnBatteries:" & .DisallowStartIfOnBatteries
					objTextFile.WriteLine "     -Enabled:" & .Enabled
					objTextFile.WriteLine "     -ExecutionTimeLimit:" & .ExecutionTimeLimit
					objTextFile.WriteLine "     -Hidden:" & .Hidden
					Select Case .MultipleInstances
						Case 0
							objTextFile.WriteLine "     -MultipleInstances:" & .MultipleInstances & " = Run in Parallel"
						Case 1
							objTextFile.WriteLine "     -MultipleInstances:" & .MultipleInstances & " = Add to Queue"
						Case 2
							objTextFile.WriteLine "     -MultipleInstances:" & .MultipleInstances & " = Ignore New"
					End Select
					objTextFile.WriteLine "     -Priority:" & .Priority & " (0=High / 10= Low)"
					objTextFile.WriteLine "     -RestartCount:" & .RestartCount 
					objTextFile.WriteLine "     -RestartInterval:" & .RestartInterval
					objTextFile.WriteLine "     -RunOnlyIfIdle:" & .RunOnlyIfIdle
					objTextFile.WriteLine "     -RunOnlyIfNetworkAvailable:" & .RunOnlyIfNetworkAvailable
					objTextFile.WriteLine "     -StartWhenAvailable:" & .StartWhenAvailable 
					objTextFile.WriteLine "     -StopIfGoingOnBatteries:" & .StopIfGoingOnBatteries 
					objTextFile.WriteLine "     -WakeToRun:" & .WakeToRun 
					
					'http://msdn.microsoft.com/en-us/library/windows/desktop/aa380669%28v=vs.85%29.aspx
					Set objIdleSettings = .IdleSettings 
					objTextFile.WriteLine "     -IDLE SETTINGS"
					With objIdleSettings
						objTextFile.WriteLine "       -IdleDuration:" & .IdleDuration
						objTextFile.WriteLine "       -RestartOnIdle:" & .RestartOnIdle
						objTextFile.WriteLine "       -StopOnIdleEnd:" & .StopOnIdleEnd
						objTextFile.WriteLine "       -WaitTimeout:" & .WaitTimeout
					End With 'objIdleSettings

					'http://msdn.microsoft.com/en-us/library/windows/desktop/aa382067%28v=vs.85%29.aspx
					Set objTaskNetworkSettings = .NetworkSettings 
					objTextFile.WriteLine "     -NETWORK SETTINGS"
					With objTaskNetworkSettings
						objTextFile.WriteLine "       -ID:" & .Id
						objTextFile.WriteLine "       -Name:" & .Name
					End With 'objTaskNetworkSettings
				End With 'objTaskSettings

				'http://msdn.microsoft.com/en-us/library/windows/desktop/aa383868%28v=vs.85%29.aspx
				Set colTaskTriggers = .Triggers
				For Each objTaskTrigger in colTaskTriggers
					objTextFile.WriteLine "   -TRIGGER"
					With objTaskTrigger
						objTextFile.WriteLine "     -Enabled:" & .Enabled
						objTextFile.WriteLine "     -Id:" & .Id
						objTextFile.WriteLine "     -StartBoundary:" & .StartBoundary
						objTextFile.WriteLine "     -EndBoundary:" & .EndBoundary
						objTextFile.WriteLine "     -ExecutionTimeLimit:" & .ExecutionTimeLimit
						Select Case .Type
							Case 0
								objTextFile.WriteLine "     -Type:" & .Type & " = Event"
								objTextFile.WriteLine "     -Delay:" & .Delay
								objTextFile.WriteLine "     -Subscription :" & .Subscription 
							Case 1
								objTextFile.WriteLine "     -Type:" & .Type & " = Time"
							Case 2
								objTextFile.WriteLine "     -Type:" & .Type & " = Daily"
								objTextFile.WriteLine "     -DaysInterval:" & .DaysInterval
							Case 3
								objTextFile.WriteLine "     -Type:" & .Type & " = Weekly"
								objTextFile.WriteLine "     -WeeksInterval:" & .WeeksInterval
								objTextFile.WriteLine "     -DaysOfWeek:" & .DaysOfWeek & "=" & ConvertDaysOfWeek(.DaysOfWeek)
							Case 4
								objTextFile.WriteLine "     -Type:" & .Type & " = Monthly"
								objTextFile.WriteLine "     -DaysOfMonth:" & .DaysOfMonth & "=" & ConvertDaysOfMonth(.DaysOfMonth)
								objTextFile.WriteLine "     -MonthsOfYear:" & .MonthsOfYear & "=" & ConvertMonthsofYear(.MonthsOfYear)
								objTextFile.WriteLine "     -RandomDelay:" & .RandomDelay 
								objTextFile.WriteLine "     -RunOnLastDayOfMonth :" & .RunOnLastDayOfMonth  
							Case 5
								objTextFile.WriteLine "     -Type:" & .Type & " = Monthly on Specific Day"
								objTextFile.WriteLine "     -DaysOfWeek:" & .DaysOfWeek & "=" & ConvertDaysOfWeek(.DaysOfWeek)
								objTextFile.WriteLine "     -MonthsOfYear:" & .MonthsOfYear & "=" & ConvertMonthsofYear(.MonthsOfYear)
								objTextFile.WriteLine "     -RandomDelay:" & .RandomDelay 
								objTextFile.WriteLine "     -RunOnLastWeekOfMonth :" & .RunOnLastWeekOfMonth  
								objTextFile.WriteLine "     -WeeksOfMonth:" & .WeeksOfMonth & "=" & ConvertWeeksOfMonth(.WeeksOfMonth)
							Case 6
								objTextFile.WriteLine "     -Type:" & .Type & " = When Computer is idle"
							Case 7
								objTextFile.WriteLine "     -Type:" & .Type & " = When Task is registered"
								objTextFile.WriteLine "     -Delay:" & .Delay
							Case 8
								objTextFile.WriteLine "     -Type:" & .Type & " = Boot"
								objTextFile.WriteLine "     -Delay:" & .Delay
							Case 9
								objTextFile.WriteLine "     -Type:" & .Type & " = Logon"
								objTextFile.WriteLine "     -Delay:" & .Delay
								objTextFile.WriteLine "     -UserId:" & .UserId
							Case 11 
								objTextFile.WriteLine "     -Type:" & .Type & " = Session State Change"
								Select Case .StateChange
									Case 0
										objTextFile.WriteLine "     -LogonType:" & .StateChange & " = None"
									Case 1
										objTextFile.WriteLine "     -LogonType:" & .StateChange & " = User Session Connect to Local  Computer"
									Case 2
										objTextFile.WriteLine "     -LogonType:" & .StateChange & " = User Session Disconnect from Local Computer"
									Case 3
										objTextFile.WriteLine "     -LogonType:" & .StateChange & " = User Session Connect to Remote Computer"
									Case 4
										objTextFile.WriteLine "     -LogonType:" & .StateChange & " = User Session Disconnect from Remote Computer"
									Case 7
										objTextFile.WriteLine "     -LogonType:" & .StateChange & " = On Workstation Lock"
									Case 8
										objTextFile.WriteLine "     -LogonType:" & .StateChange & " = On Workstation Unlock"
								End Select
								objTextFile.WriteLine "     -Delay:" & .Delay
								objTextFile.WriteLine "     -UserId:" & .UserId
						End Select
						
						Set objTaskRepitition = .Repetition
						objTextFile.WriteLine "     -REPITITION"
						With objTaskRepitition
							objTextFile.WriteLine "       -Duration:" & .Duration
							objTextFile.WriteLine "       -Interval:" & .Interval
							objTextFile.WriteLine "       -StopAtDurationEnd:" & .StopAtDurationEnd
						End With 'objTaskRepitition
					End With 'objTaskTrigger
				Next 'objTaskTrigger

			End With 'objTaskDefinition
			objTextFile.WriteLine
			objTextFile.WriteLine " -XML:" & .XML
			objTextFile.WriteLine "****************************************"
			objTextFile.WriteLine
		End With 'objTask
    Next
End If

objTextFile.Close
msgbox "Done"

Function ConvertDaysOfMonth(BitValue)
	Dim strMsg

	If BitValue AND &H01& Then strMsg = "1"
	If BitValue AND &H02& Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "2"
	End If
	If BitValue AND &H04&  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "3"
	End if
	If BitValue AND &H08&  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "4"
	End if
	If BitValue AND &H10&  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "5"
	End if
	If BitValue AND &H20&  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "6"
	End if
	If BitValue AND &H40&  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "7"
	End if
	If BitValue AND &H80&  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "8"
	End if
	If BitValue AND &H100&  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "9"
	End if
	If BitValue AND &H200&  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "10"
	End if
	If BitValue AND &H400&  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "11"
	End if
	If BitValue AND &H800&  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "12"
	End if
	If BitValue AND &H1000&  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "13"
	End if
	If BitValue AND &H2000&  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "14"
	End if
	If BitValue AND &H4000&  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "15"
	End if
	If BitValue AND &H8000&  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "16"
	End if
	If BitValue AND &H10000&  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "17"
	End if
	If BitValue AND &H20000&  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "18"
	End if
	If BitValue AND &H40000&  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "19"
	End if
	If BitValue AND &H80000&  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "20"
	End if
	If BitValue AND &H100000&  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "21"
	End if
	If BitValue AND &H200000&  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "22"
	End if
	If BitValue AND &H400000&  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "23"
	End if
	If BitValue AND &H800000&  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "24"
	End if
	If BitValue AND &H1000000&  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "25"
	End if
	If BitValue AND &H2000000&  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "26"
	End if
	If BitValue AND &H4000000&  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "27"
	End if
	If BitValue AND &H8000000&  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "28"
	End if
	If BitValue AND &H10000000&  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "29"
	End if
	If BitValue AND &H20000000&  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "30"
	End if
	If BitValue AND &H40000000&  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "31"
	End if
	If BitValue AND &H80000000&  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "LAST"
	End if

	ConvertDaysOfMonth = strMsg
End Function

Function ConvertDaysOfWeek(BitValue)
	Dim strMsg

	If BitValue AND 1 Then strMsg = "Sunday"
	If BitValue AND 2 Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "Monday"
	End If
	If BitValue AND 4  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "Tuesday"
	End if
	If BitValue AND 8  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "Wednesday"
	End if
	If BitValue AND 16  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "Thursday"
	End if
	If BitValue AND 32  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "Friday"
	End if
	If BitValue AND 64  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "Saturday"
	End if
	
	ConvertDaysOfWeek = strMsg
End Function

Function ConvertMonthsofYear(BitValue)
	Dim strMsg

	If BitValue AND  1 Then strMsg = "January"
	If BitValue AND 2 Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "February"
	End If
	If BitValue AND 4  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "March"
	End if
	If BitValue AND 8  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "April"
	End if
	If BitValue AND 16  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "May"
	End if
	If BitValue AND 32  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "June"
	End if
	If BitValue AND 64  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "July"
	End if
	If BitValue AND 128  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "August"
	End if
	If BitValue AND 256  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "September"
	End if
	If BitValue AND 512  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "October"
	End if
	If BitValue AND 1024  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "November"
	End if
	If BitValue AND 2048  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "December"
	End if

	ConvertMonthsofYear = strMsg
End Function

Function ConvertWeeksOfMonth(BitValue)
	Dim strMsg

	If BitValue AND  1 Then strMsg = "First"
	If BitValue AND 2 Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "Second"
	End If
	If BitValue AND 4  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "Third"
	End if
	If BitValue AND 8  Then
		If len(strMsg) > 0 Then strMsg = strMsg & ", "
		strMsg = strMsg & "Fourth"
	End if

	ConvertWeeksOfMonth = strMsg
End Function

Open in new window


For those that want a little more detail, I will break down the code:

Lines 25-26:
First you have to instantiate an object to access the tasks.  For Task Scheduler 2.0 you also have to run the connect method.

Line 29:
In Task Scheduler 2.0 you can group tasks together into different folders.  With this object you can limit you query to just a certain folder or just use “\” to get the root folder.

Line 32:
Grab the collection of Tasks.  If you use an enumeration of 0 for GetTasks, you will get a list of all tasks including those that are “hidden.”  An enumeration of 1 will not show the hidden tasks.  You can also use .GetTask to get a specific task.

Lines 40-59:
Make sure something was returned and then start looping through the collection.  First I list the basic Task Values for the found task (Name, Enabled Flag, etc.)

Lines 62-106:
Get access to the the complex Definition object. Then determine what kind of action is taken by the task (command line, Handler, Email, or Message Box) and show the properties that are populated by that type of task.

Lines 109-140:
The Principal object shows the User and Privileges of the task from the General Tab.  One key element here is the Run Level which equates to the "Run with highest privileges" check box.

Lines 143-155:
Registration Information about who or what created this task.

Lines 158-211:
Values from the Settings tab of the Task.  

Lines 215-283:
This will show each Trigger created on the Triggers tab.  Some of the triggers can only be created in code and this will show all types (Event, Time, Daily, Weekly, Monthly, Monthly on specific days, when the computer is idle, when the task is set up, Boot, Logon, and session state change).  Many of the values in this section are stored as bit flags (see this excellent EE article to understand bit flags more - http://www.experts-exchange.com/Programming/Misc/A_1842-Binary-Bit-Flags-Tutorial-and-Usage-Tips.html).  Lines 307-541 contains four conversion routines that convert those bit flags into meaningful text based on the enumeration documentation for Task Scheduler.

Lines 285-291:
Each trigger also has a repeat setting - once the task is started, should it keep running and how often.

Line 297:
Each task is actually represented in an XML document.  Here I print the full XML.  You could just grab this object and through it into a DomDocument and parse out the settings from the full XML.
1
Comment
Author:ltlbearand3
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
 

Administrative Comment

by:Eric AKA Netminder
ltlbearand3,

Congratulations! Your article has been published.

ericpete
Page Editor
0
 

Expert Comment

by:PolitoT
First, EXCELLENT article/explanation. Your articles on Task Scheduler 2 are the only ones I've found. Any thoughts on what could be done to list tasks created by other local and/or domain accounts? The script works on my computer but does not show all tasks in the "root". Some tasks that aren't enumerated use a domain service account (which I don't have permission to).

Windows 7 SP 1, 32 bit.  I'm an administrator on the computer, administrator for computers in my OU (not full domain/forest administrator).

Experts Exchange owes you one because you're the only reason I've decided to get a paid account.
0
 
LVL 20

Author Comment

by:ltlbearand3
Polito T,

I am glad you found the article helpful and thanks for the kind words.  

Concerning your question, you would be best suited to handle this as a question in the VBscript forum.  That is monitored by some really smart people, who can also jump in to help out if the article author is not available.  I don't have an easy answer to your questions at hand.  It may take some back and forth work that will best be done as forum question.  I have some questions about what you see and want to keep a lengthy discussion out of the article comments.  if you do open a question, you could post a link here and I can go check it out.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Join & Write a Comment

In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month