Registration Trigger Example (Scripting)
This scripting example shows how to create a task that is scheduled to execute Notepad when a task is registered. The task contains a registration trigger that specifies a start boundary and an end boundary for the task. The start boundary specifies when the trigger is activated. The task also contains an action that specifies the task to execute Notepad.
Note
When a task with a registration trigger is updated, the task will execute after the update occurs.
The following procedure describes how to schedule an executable such as Notepad to start when a task is registered.
To schedule Notepad to start when a task is registered
- Create a TaskService object. This object allows you to create the task in a specified folder.
- Get a task folder and create a task. Use the TaskService.GetFolder method to get the folder where the task is stored and the TaskService.NewTask method to create the TaskDefinition object that represents the task.
- Define information about the task using the TaskDefinition object. Use the TaskDefinition.Settings property to define the settings that determine how the Task Scheduler service performs the task and the TaskDefinition.RegistrationInfo property to define the information that describes the task.
- Create a registration trigger using the TaskDefinition.Triggers property. This property provides access to the TriggerCollection object. Use the TriggerCollection.Create method (specifying the type of trigger that you want to create) to create a registration trigger.
- Create an action for the task to execute by using the TaskDefinition.Actions property. This property provides access to the ActionCollection object. Use the ActionCollection.Create method to specify the type of action that you want to create. This example uses an ExecAction object, which represents an action that starts an executable.
- Register the task using the TaskFolder.RegisterTaskDefinition method.
The following VBScript example shows how to create a task that schedules Notepad to execute when the task is registered.
'---------------------------------------------------------
' This sample schedules a task to start notepad.exe when
' the task is registered.
'---------------------------------------------------------
' A constant that specifies a registration trigger.
const TriggerTypeRegistration = 7
' A constant that specifies an executable action.
const ActionTypeExecutable = 0
'********************************************************
' Create the TaskService object.
Set service = CreateObject("Schedule.Service")
call service.Connect()
'********************************************************
' Get a folder to create a task definition in.
Dim rootFolder
Set rootFolder = service.GetFolder("\")
' The taskDefinition variable is the TaskDefinition object.
Dim taskDefinition
' The flags parameter is 0 because it is not supported.
Set taskDefinition = service.NewTask(0)
'********************************************************
' Define information about the task.
' Set the registration info for the task by
' creating the RegistrationInfo object.
Dim regInfo
Set regInfo = taskDefinition.RegistrationInfo
regInfo.Description = "Start Notepad when the task is registered."
regInfo.Author = "Author Name"
' Set the task setting info for the Task Scheduler by
' creating a TaskSettings object.
Dim settings
Set settings = taskDefinition.Settings
settings.StartWhenAvailable = True
'********************************************************
' Create a registration trigger.
Dim triggers
Set triggers = taskDefinition.Triggers
Dim trigger
Set trigger = triggers.Create(TriggerTypeRegistration)
trigger.ExecutionTimeLimit = "PT5M" 'Five minutes
trigger.Id = "RegistrationTriggerId"
'***********************************************************
' Create the action for the task to execute.
' Add an action to the task. The action executes Notepad.
Dim Action
Set Action = taskDefinition.Actions.Create( ActionTypeExecutable )
Action.Path = "C:\Windows\System32\notepad.exe"
WScript.Echo "Task definition created. About to submit the task..."
'***********************************************************
' Register (create) the task.
call rootFolder.RegisterTaskDefinition( _
"Test Registration Trigger", taskDefinition, 6, , , 3)
WScript.Echo "Task submitted."
Related topics