共用方式為


開機觸發程式範例 (文稿)

此腳本範例示範如何在系統開機時建立排程執行記事本的工作。 工作包含開機觸發程式,指定啟動界限和工作在開機後啟動的延遲時間。 工作也包含一個動作,指定要執行記事本的工作。 工作是使用本地服務帳戶註冊為執行工作的安全性內容。

下列程式描述如何排程記事本等可執行檔,以在系統開機時啟動。

若要排程記事本在系統開機時啟動

  1. 建立 TaskService 物件。 這個物件可讓您在指定的資料夾中建立工作。
  2. 取得工作資料夾並建立工作。 使用 TaskService.GetFolder 方法來取得工作儲存所在的資料夾,以及 TaskService.NewTask 方法來建立代表工作的 TaskDefinition 物件。
  3. 使用 TaskDefinition 物件定義工作的相關信息。 使用 TaskDefinition.Settings 屬性來定義可決定 Task Scheduler 服務如何執行工作的設定,以及 TaskDefinition.RegistrationInfo 屬性來定義描述工作的資訊。
  4. 使用 TaskDefinition.Triggers 屬性建立登入觸發程式。 此屬性提供 TriggerCollection 物件的存取權。 使用 TriggerCollection.Create 方法(指定要建立的觸發程式類型)來建立開機觸發程式。 當您建立觸發程式時,請設定觸發程式的 StartBoundaryEndBoundary 屬性,以啟動和停用觸發程式。 您也可以為開機觸發程式的 Delay 屬性指定值。
  5. 使用 taskDefinition.Actions 屬性,建立要執行之工作的動作。 這個屬性可讓您存取 actionCollection物件。 使用 ActionCollection.Create 方法來指定您想要建立的動作類型。 此範例會使用 ExecAction 物件,代表啟動可執行文件的動作。
  6. 使用 TaskFolder.RegisterTaskDefinition 方法來註冊工作。 工作是使用本地服務帳戶註冊為執行工作的安全性內容。

下列 VBScript 範例示範如何在系統開機後排程工作以執行記事本 30 秒。

'---------------------------------------------------------
' This sample schedules a task to start notepad.exe 30 seconds after
' the system is booted.
'---------------------------------------------------------

' A constant that specifies a boot trigger.
const TriggerTypeBoot = 8
' 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 = "Task will execute Notepad when " & _
    "the computer is booted."
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 boot trigger.
Dim triggers
Set triggers = taskDefinition.Triggers

Dim trigger
Set trigger = triggers.Create(TriggerTypeBoot)

' Trigger variables that define when the trigger is active.
Dim startTime, endTime
startTime = "2006-05-02T10:49:02"
endTime = "2006-05-02T10:52:02"

WScript.Echo "startTime :" & startTime
WScript.Echo "endTime :" & endTime

trigger.StartBoundary = startTime
trigger.EndBoundary = endTime
trigger.ExecutionTimeLimit = "PT5M"    ' Five minutes
trigger.Id = "BootTriggerId"
trigger.Delay = "PT30S"                ' 30 Seconds   

'***********************************************************
' 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.
const createOrUpdateTask = 6
call rootFolder.RegisterTaskDefinition( _
    "Test Boot Trigger", taskDefinition, createOrUpdateTask, _
    "Local Service", , 5)

WScript.Echo "Task submitted."

使用工作排程器