次の方法で共有


日次トリガーの例 (スクリプト)

このスクリプトの例では、毎日午前 8 時にメモ帳を実行するタスクを作成する方法を示します。 タスクには、トリガーをアクティブ化し、タスクを実行する時刻を指定する開始境界、タスクが毎日実行されることを指定するトリガー間隔、およびトリガーを非アクティブ化するための終了境界を指定する日次トリガーが含まれています。 この例では、タスクを繰り返すトリガーの繰り返しパターンを設定する方法も示します。 タスクには、メモ帳を実行する実行可能アクションも含まれています。

次の手順では、毎日午前 8 時に実行可能ファイルを開始するようにタスクをスケジュールする方法について説明します。 (これらの手順は、コード例に含まれているコード コメントに対応しています)。

毎日午前 8 時に開始するようにメモ帳をスケジュールするには

  1. TaskService オブジェクトを作成します。 このオブジェクトを使用すると、指定したフォルダーにタスクを作成できます。
  2. タスク フォルダーを取得し、タスクを作成します。 TaskService.GetFolder メソッドを使用して、タスクが格納されているフォルダーを取得し、タスクを表す TaskDefinition オブジェクトを作成する TaskService.NewTask メソッドを使用します。
  3. TaskDefinition オブジェクトを使用して、タスクに関する情報を定義します。 TaskDefinition.Settings プロパティを使用して、タスク スケジューラ サービスがタスクを実行する方法を決定する設定を定義し、タスクを説明する情報を定義する TaskDefinition.RegistrationInfo プロパティを定義します。
  4. TaskDefinition.Triggers プロパティを使用して、毎日のトリガーを作成します。 このプロパティは、トリガーの作成に使用される TriggerCollection オブジェクトへのアクセスを提供します。 TriggerCollection.Create メソッド (作成するトリガーの種類を指定) を使用して、毎日のトリガーを作成します。 トリガーを作成するときに、トリガーをアクティブ化するように開始境界を設定し、タスクが実行される時刻、日の間隔、およびトリガーを非アクティブ化する終了境界を指定します。 次の例は、タスクを繰り返すトリガーの繰り返しパターンを設定する方法を示しています。
  5. TaskDefinition.Actions プロパティを使用して、実行するタスクのアクションを作成します。 このプロパティは、アクションの作成に使用する ActionCollection オブジェクトへのアクセスを提供します。 ActionCollection.Create メソッドを使用して、作成するアクションの種類を指定します。 この例では、コマンド ライン操作を実行するアクションを表す ExecAction オブジェクトを使用します。
  6. TaskFolder.RegisterTaskDefinition メソッドを使用してタスクを登録します。 この例では、タスクは毎日午前 8 時にメモ帳を開始します。

次の VBScript の例は、毎日午前 8 時にメモ帳を実行するようにタスクをスケジュールする方法を示しています。

'------------------------------------------------------------------
' This sample schedules a task to start on a daily basis.
'------------------------------------------------------------------

' A constant that specifies a daily trigger.
const TriggerTypeDaily = 2
' A constant that specifies an executable action.
const ActionTypeExec = 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 at 8:00AM daily"
regInfo.Author = "Administrator"

' Set the task setting info for the Task Scheduler by
' creating a TaskSettings object.
Dim settings
Set settings = taskDefinition.Settings
settings.Enabled = True
settings.StartWhenAvailable = True
settings.Hidden = False

'********************************************************
' Create a daily trigger. Note that the start boundary 
' specifies the time of day that the task starts and the 
' interval specifies what days the task is run.
Dim triggers
Set triggers = taskDefinition.Triggers

Dim trigger
Set trigger = triggers.Create(TriggerTypeDaily)

' Trigger variables that define when the trigger is active 
' and the time of day that the task is run. The format of 
' this time is YYYY-MM-DDTHH:MM:SS
Dim startTime, endTime

Dim time
startTime = "2006-05-02T08:00:00"  'Task runs at 8:00 AM
endTime = "2015-05-02T08:00:00"

WScript.Echo "startTime :" & startTime
WScript.Echo "endTime :" & endTime

trigger.StartBoundary = startTime
trigger.EndBoundary = endTime
trigger.DaysInterval = 1    'Task runs every day.
trigger.Id = "DailyTriggerId"
trigger.Enabled = True

' Set the task repetition pattern for the task.
' This will repeat the task 5 times.
Dim repetitionPattern
Set repetitionPattern = trigger.Repetition
repetitionPattern.Duration = "PT4M"
repetitionPattern.Interval = "PT1M"

'***********************************************************
' Create the action for the task to execute.

' Add an action to the task to run notepad.exe.
Dim Action
Set Action = taskDefinition.Actions.Create( ActionTypeExec )
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 Daily Trigger", taskDefinition, 6, , , 3)

WScript.Echo "Task submitted."

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