次の方法で共有


ログオン トリガーの例 (スクリプト)

このスクリプトの例では、ユーザーがログオンしたときにメモ帳を実行するようにスケジュールされたタスクを作成する方法を示します。 タスクには、開始するタスクの開始境界とユーザーを指定するユーザー識別子を指定するログオン トリガーが含まれています。 タスクは、タスクを実行するためのセキュリティ コンテキストとして Administrators グループを使用して登録されます。

次の手順では、指定したユーザーがログオンしたときにメモ帳などの実行可能ファイルを開始するようにスケジュールする方法について説明します。

ユーザーが にログオンしたときにメモ帳を開始するようにスケジュールするには

  1. TaskService オブジェクトを作成します。 このオブジェクトを使用すると、指定したフォルダーにタスクを作成できます。
  2. タスク フォルダーを取得し、タスクを作成します。 TaskService.GetFolder メソッドを使用して、タスクが格納されているフォルダーを取得し、タスクを表す TaskDefinition オブジェクトを作成する TaskService.NewTask メソッドを使用します。
  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."

タスク スケジューラ を使用した