Share via


登入觸發程式範例 (腳本)

此腳本範例示範如何在使用者登入時建立排程執行記事本的工作。 工作包含登入觸發程式,指定要啟動之工作的開始界限,以及指定要指定使用者的使用者識別碼。 工作會使用 Administrators 群組作為執行工作的安全性內容進行註冊。

下列程式描述如何排程可執行檔,例如記事本,以在指定的使用者登入時啟動。

若要排程記事本以在使用者登入時啟動

  1. 建立 TaskService 物件。 這個物件可讓您在指定的資料夾中建立工作。
  2. 取得工作資料夾並建立工作。 使用 TaskService.GetFolder 方法來取得工作儲存所在的資料夾,以及 TaskService.NewTask 方法可建立代表工作的 TaskDefinition 物件。
  3. 使用 TaskDefinition 物件定義工作的相關資訊。 使用 TaskDefinition.Settings 屬性來定義設定,以決定工作排程器服務如何執行工作和 TaskDefinition.RegistrationInfo 屬性來定義描述工作的資訊。
  4. 使用 TaskDefinition.Triggers 屬性建立登入觸發程式。 此屬性提供 TriggerCollection 物件的存取權。 使用 TriggerCollection.Create 方法 (指定您要建立) 建立登入觸發程式的觸發程式類型。 當您建立觸發程式時,請設定觸發程式的開始界限和結束界限,以啟用和停用觸發程式。 您必須設定觸發程式的 UserId 屬性,讓工作動作在啟動界限之後登入時排程執行。
  5. 使用 TaskDefinition.Actions 屬性建立要執行之工作的動作。 此屬性提供 ActionCollection 物件的存取權。 使用 ActionCollection.Create 方法可指定您想要建立的動作類型。 這個範例使用 ExecAction 物件,代表啟動可執行檔的動作。
  6. 使用 TaskFolder.RegisterTaskDefinition 方法註冊 工作。 此範例會註冊工作,使其使用 Administrators 群組作為執行工作的安全性內容。

下列 VBScript 範例示範如何在使用者登入時排程工作來執行記事本。

'---------------------------------------------------------
' This sample schedules a task to start notepad.exe when a user logs on.
'---------------------------------------------------------

' A constant that specifies a logon trigger.
const TriggerTypeLogon = 9
' 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 a " & _
    "specified user logs on."
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 logon trigger.
Dim triggers
Set triggers = taskDefinition.Triggers

Dim trigger
Set trigger = triggers.Create(TriggerTypeLogon)

' 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 = "LogonTriggerId"
trigger.UserId = "DOMAIN\UserName"   ' Must be a valid user account   

'***********************************************************
' 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 Logon Trigger", taskDefinition, createOrUpdateTask, _
    "Builtin\Administrators", , 4)

WScript.Echo "Task submitted."

使用工作排程器