註冊觸發程式範例 (文稿)
此腳本範例示範如何在登錄工作時建立排程執行記事本的工作。 工作包含註冊觸發程式,指定工作的開始界限和結束界限。 啟動界限會指定觸發程式啟動時間。 工作也包含一個動作,指定要執行記事本的工作。
注意
更新具有註冊觸發程式的工作時,工作會在更新發生之後執行。
下列程式描述如何排程可執行檔,例如記事本,以在工作註冊時啟動。
若要排程記事本在註冊工作時啟動
- 建立 TaskService 物件。 這個物件可讓您在指定的資料夾中建立工作。
- 取得工作資料夾並建立工作。 使用 TaskService.GetFolder 方法來取得工作儲存所在的資料夾,以及 TaskService.NewTask 方法來建立代表工作的 TaskDefinition 物件。
- 使用 TaskDefinition 物件定義工作的相關信息。 使用 TaskDefinition.Settings 屬性來定義可決定 Task Scheduler 服務如何執行工作的設定,以及 TaskDefinition.RegistrationInfo 屬性來定義描述工作的資訊。
- 使用 TaskDefinition.Triggers 屬性建立註冊觸發程式。 此屬性提供 TriggerCollection 物件的存取權。 使用 TriggerCollection.Create 方法(指定要建立的觸發程式類型)來建立註冊觸發程式。
- 使用 taskDefinition.Actions 屬性,建立要執行之工作的動作。 這個屬性可讓您存取 actionCollection物件。 使用 ActionCollection.Create 方法來指定您想要建立的動作類型。 此範例會使用 ExecAction 物件,代表啟動可執行文件的動作。
- 使用 TaskFolder.RegisterTaskDefinition 方法來註冊工作。
下列 VBScript 範例示範如何建立工作,以排程記事本在註冊工作時執行。
'---------------------------------------------------------
' 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."
相關主題