次の方法で共有


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

このスクリプトの例では、タスクの登録時にメモ帳を実行するようにスケジュールされたタスクを作成する方法を示します。 タスクには、タスクの開始境界と終了境界を指定する登録トリガーが含まれています。 開始境界は、トリガーがアクティブ化されるタイミングを指定します。 タスクには、メモ帳を実行するタスクを指定するアクションも含まれています。

注意

登録トリガーを持つタスクが更新されると、更新後にタスクが実行されます。

 

次の手順では、タスクの登録時にメモ帳などの実行可能ファイルを開始するようにスケジュールする方法について説明します。

タスクが登録されたときにメモ帳を開始するようにスケジュールするには

  1. TaskService オブジェクトを作成します。 このオブジェクトを使用すると、指定したフォルダーにタスクを作成できます。
  2. タスク フォルダーを取得し、タスクを作成します。 TaskService.GetFolder メソッドを使用して、タスクが格納されているフォルダーを取得し、TaskService.NewTask メソッドを使用して、タスクを表す TaskDefinition オブジェクトを作成します。
  3. TaskDefinition オブジェクトを使用して、タスクに関する情報を定義します。 TaskDefinition.Settings プロパティを使用して、タスク スケジューラ サービスによるタスクの実行方法を決定する設定を定義し、TaskDefinition.RegistrationInfo プロパティを使用して、タスクを説明する情報を定義します。
  4. TaskDefinition.Triggers プロパティを使用して登録トリガーを作成します。 このプロパティは、 TriggerCollection オブジェクトへのアクセスを提供します。 TriggerCollection.Create メソッド (作成するトリガーの種類を指定) を使用して、登録トリガーを作成します。
  5. TaskDefinition.Actions プロパティを使用して、実行するタスクのアクションを作成します。 このプロパティは、 ActionCollection オブジェクトへのアクセスを提供します。 ActionCollection.Create メソッドを使用して、作成するアクションの種類を指定します。 この例では、実行可能ファイルを開始するアクションを表す ExecAction オブジェクトを使用します。
  6. TaskFolder.RegisterTaskDefinition メソッドを使用してタスクを登録します。

次の VBScript の例は、タスクの登録時にメモ帳の実行をスケジュールするタスクを作成する方法を示しています。

'---------------------------------------------------------
' This sample schedules a task to start notepad.exe when
' the task is registered.
'---------------------------------------------------------

' A constant that specifies a registration trigger.
const TriggerTypeRegistration = 7
' 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 = "Start Notepad when the task is registered."
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 registration trigger.
Dim triggers
Set triggers = taskDefinition.Triggers

Dim trigger
Set trigger = triggers.Create(TriggerTypeRegistration)

trigger.ExecutionTimeLimit = "PT5M"    'Five minutes
trigger.Id = "RegistrationTriggerId"   

'***********************************************************
' 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.

call rootFolder.RegisterTaskDefinition( _
    "Test Registration Trigger", taskDefinition, 6, , , 3)

WScript.Echo "Task submitted."

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