Displaying Task Names and States (Scripting)

This scripting example shows how to enumerate tasks in a task folder and display property values from each task.

The following procedure describes how to display task names and states for all the tasks in a task folder.

To display task names and state for all the tasks in a task folder

  1. Create the TaskService object.

    This object allows you to connect to the Task Scheduler service and access a specific task folder.

  2. Get a task folder that holds the tasks you want information about.

    Use the TaskService.GetFolder method to get the folder.

  3. Get the collection of tasks from the folder.

    Use the TaskFolder.GetTasks method to get the collection of tasks (RegisteredTaskCollection).

  4. Get the number of tasks in the collection and enumerate through each task in the collection.

    Use the RegisteredTaskCollection collection of objects to get a RegisteredTask object instance. Each instance will contain a task in the collection. You can then display the information (property values) from each registered task.

The following VBScript example shows how to enumerate through a collection of registered tasks in the root task folder and display the name and state for each task.

'---------------------------------------------------------
' This sample enumerates through the tasks on the local computer and
' displays their name and state.
'---------------------------------------------------------


' Create the TaskService object.
Set service = CreateObject("Schedule.Service")
call service.Connect()

' Get the task folder that contains the tasks. 
Dim rootFolder
Set rootFolder = service.GetFolder("\")
 
Dim taskCollection
Set taskCollection = rootFolder.GetTasks(0)

Dim numberOfTasks
numberOfTasks = taskCollection.Count

If numberOfTasks = 0 Then 
    Wscript.Echo "No tasks are registered."
Else
    WScript.Echo "Number of tasks registered: " & numberOfTasks
    
    Dim registeredTask
    For Each registeredTask In taskCollection
        WScript.Echo "Task Name: " & registeredTask.Name
    
        Dim taskState 
        Select Case registeredTask.State 
            Case "0"
                taskState = "Unknown"
            Case "1"
                taskState = "Disabled"
            Case "2"
                taskState = "Queued"
            Case "3"
                taskState = "Ready"
            Case "4"
                taskState = "Running"
        End Select

        WScript.Echo "    Task State: " & taskState
    Next
End If

Using the Task Scheduler