ブート トリガーの例 (スクリプト作成)

このスクリプトの例では、システムの起動時にメモ帳を実行するようにスケジュールされたタスクを作成する方法を示します。 タスクには、システムの起動後にタスクを開始するための開始境界と遅延時間を指定するブート トリガーが含まれています。 タスクには、メモ帳を実行するタスクを指定するアクションも含まれています。 タスクは、タスクを実行するためのセキュリティ コンテキストとしてローカル サービス アカウントを使用して登録されます。

次の手順では、システムの起動時にメモ帳などの実行可能ファイルを起動するようにスケジュールする方法について説明します。

システムの起動時にメモ帳を起動するようにスケジュールするには

  1. TaskService オブジェクトを作成します。 このオブジェクトを使用すると、指定したフォルダーにタスクを作成できます。
  2. タスク フォルダーを取得し、タスクを作成します。 TaskService.GetFolder メソッドを使用して、タスクが格納されているフォルダーを取得し、TaskService.NewTask メソッドを使用して、タスクを表す TaskDefinition オブジェクトを作成します。
  3. TaskDefinition オブジェクトを使用して、タスクに関する情報を定義します。 TaskDefinition.Settings プロパティを使用して、タスク スケジューラ サービスによるタスクの実行方法を決定する設定を定義し、TaskDefinition.RegistrationInfo プロパティを使用して、タスクを説明する情報を定義します。
  4. TaskDefinition.Triggers プロパティを使用してログオン トリガーを作成します。 このプロパティは、 TriggerCollection オブジェクトへのアクセスを提供します。 TriggerCollection.Create メソッド (作成するトリガーの種類を指定) を使用して、ブート トリガーを作成します。 トリガーを作成するときに、トリガーの StartBoundary プロパティと EndBoundary プロパティを設定して、トリガーをアクティブ化および非アクティブ化します。 ブート トリガーの 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."

タスク スケジューラの使用