登录触发器示例 (脚本)

此脚本示例演示如何创建在用户登录时计划执行记事本的任务。 该任务包含一个登录触发器,该触发器指定要启动的任务的起始边界,以及一个指定用户的用户标识符。 该任务是使用 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."

使用任务计划程序