VBScript和Task Scheduler 2.0:列出预定任务

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.

多年以来,我建立了自己的小代码段库,在编程或编写脚本时会参考这些库。 其中许多来自网络或我在网络上找到的摘录改编而成。 当我遇到尚未完成的事情时,我会定期添加他们。 最近,我需要使用VB脚本使用Task Scheduler 2.0。 我发现示例脚本供不应求,其中大多数没有足够的对象。 幸运的是文档很不错,但是我仍然想要一个示例脚本。 因此,我为自己构建了一个脚本,该脚本将列出所有计划的任务及其属性。 我想与其他正在寻找的人分享。

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.

Task Scheduler 2.0最初随Vista操作系统一起发布,并且应该是在较新的Microsoft操作系统中查看计划任务时使用的对象。 它将使您能够访问该版本中引入的许多新选项。 由于Task Scheduler 2.0在较旧的操作系统上不可用,因此请注意,此代码不适用于那些系统。

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.

下面的脚本将列出所有创建的任务及其选项,并将其保存到文本文件“ C:\ TaskListing.txt”。 可以在脚本开头(第7行)更新此文件名和/或路径。 我在每个部分中都添加了评论,并提供了指向该部分相关文档的链接。 该代码具有所有可能的属性和转换,因此您可以看到所有选项。 如果您只想查看某些类型的计划任务,则应该能够轻松提取所需的内容。 如果不确定任何值,请查找该部分的注释链接,以查看该对象的官方文档。

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

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

对于那些需要更多细节的人,我将分解代码:

Lines 25-26:

第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.

首先,您必须实例化一个对象才能访问任务。 对于Task Scheduler 2.0,您还必须运行connect方法。

Line 29:

第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.

在Task Scheduler 2.0中,您可以将任务分组到不同的文件夹中。 使用此对象,您可以将查询限制为仅某个文件夹,也可以仅使用“ \”获取根文件夹。

Line 32:

第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.

获取任务的集合。 如果对GetTasks使用枚举0,则将获得所有任务的列表,包括“隐藏”的任务。 枚举1不会显示隐藏的任务。 您也可以使用.GetTask来获取特定任务。

Lines 40-59:

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:

第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.

获取对复杂的Definition对象的访问。 然后确定任务(命令行,处理程序,电子邮件或消息框)采取何种操作,并显示该任务类型填充的属性。

Lines 109-140:

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.

Principal对象从“常规”选项卡显示任务的“用户”和“特权”。 此处的一个关键元素是“运行级别”,它相当于“以最高特权运行”复选框。

Lines 143-155:

143-155行:

Registration Information about who or what created this task.

关于谁或什么创建此任务的注册信息。

Lines 158-211:

158-211行:

Values from the Settings tab of the Task.  

任务的“设置”选项卡中的值。

Lines 215-283:

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 - https://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.

这将显示在“触发器”选项卡上创建的每个触发器。 某些触发器只能通过代码创建,并且将显示所有类型(事件,时间,每日,每周,每月,每月,每月在特定日期,计算机空闲,设置任务,启动,登录和会话状态更改)。 本节中的许多值都存储为位标记(请参阅这篇出色的EE文章以了解更多位标记-https: //www.experts-exchange.com/Programming/Misc/A_1842-Binary-Bit-Flags-Tutorial- and-Usage-Tips.html )。 307-541行包含四个转换例程,这些例程根据Task Scheduler的枚举文档将那些位标志转换为有意义的文本。

Lines 285-291:

第285-291行:

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

每个触发器还具有重复设置-任务启动后,是否应继续运行以及执行频率。

Line 297:

第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.

每个任务实际上都在XML文档中表示。 在这里,我将打印完整的XML。 您可以仅抓取该对象并将其放入DomDocument中,然后从完整的XML中解析出设置。

翻译自: https://www.experts-exchange.com/articles/11326/VBScript-and-Task-Scheduler-2-0-Listing-Scheduled-Tasks.html

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值