次の方法で共有


週単位のトリガーの例 (スクリプト)

このスクリプトの例では、毎週月曜日の午前 8 時にメモ帳を実行するタスクを作成する方法を示します。 タスクには、タスクの実行日時を指定する毎日のトリガーと、メモ帳を実行する実行可能アクションが含まれています。

次の手順では、毎週月曜日の午前 8 時に実行可能ファイルを開始するようにタスクをスケジュールする方法について説明します。

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

  1. TaskService オブジェクトを作成します。 このオブジェクトを使用すると、指定したフォルダーにタスクを作成できます。

  2. タスク フォルダーを取得し、タスクを作成します。 TaskService.GetFolder メソッドを使用して、タスクが格納されているフォルダーを取得し、TaskService.NewTask メソッドを使用して、タスクを表す TaskDefinition オブジェクトを作成します。

  3. TaskDefinition オブジェクトを使用して、タスクに関する情報を定義します。 TaskDefinition.Settings プロパティを使用して、タスク スケジューラ サービスによるタスクの実行方法を決定する設定を定義し、TaskDefinition.RegistrationInfo プロパティを使用して、タスクを説明する情報を定義します。

  4. TaskDefinition.Triggers プロパティを使用して週単位のトリガーを作成します。 このプロパティは、トリガーの作成に使用される TriggerCollection オブジェクトへのアクセスを提供します。

    TriggerCollection.Create メソッド (作成するトリガーの種類を指定) を使用して、毎週トリガーを作成します。

    WeeklyTrigger.StartBoundary プロパティを設定して、トリガーをアクティブにするタイミングとタスクが実行される時刻を指定します。 この例では、トリガーは 2005 年 1 月 1 日にアクティブ化され、タスクは午前 8 時に実行されます。

    WeeklyTrigger.EndBoundaryプロパティを設定して、トリガーが非アクティブ化されるタイミングを指定します。 この例では、トリガーは 2015 年 1 月 1 日に非アクティブ化されます。

    WeeklyTrigger.DaysOfWeek プロパティを設定して、タスクを実行する曜日を指定します。 この例では、タスクは月曜日に実行されます。

    WeeklyTrigger.WeeksIntervalプロパティを設定して、スケジュール内の週の間隔を指定します。 この例では、タスクは毎週実行されます。

  5. TaskDefinition.Actions プロパティを使用して、実行するタスクのアクションを作成します。 このプロパティは、アクションの作成に使用される ActionCollection オブジェクトへのアクセスを提供します。 ActionCollection.Create メソッドを使用して、作成するアクションの種類を指定します。 この例では、コマンドライン操作を実行するアクションを表す ExecAction オブジェクトを使用します。

  6. TaskFolder.RegisterTaskDefinition メソッドを使用してタスクを登録します。 この例では、タスクは毎週月曜日の午前 8 時にメモ帳を開始します。

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

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

' A constant that specifies a weekly trigger.
const TriggerTypeWeekly = 3
' 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 weekly."
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 weekly trigger. Note that the start boundary 
' specifies the time of day that the task starts, the 
' day-of-week specfies on what day of the week the task 
' runs, and the interval specifies what weeks the task runs.
Dim triggers
Set triggers = taskDefinition.Triggers

Dim trigger
Set trigger = triggers.Create(TriggerTypeWeekly)

' Trigger variables that define when the trigger is active 
' and the time of day that the task is run. The format of 
' this tims 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.DaysOfWeek = 1
trigger.WeeksInterval = 1    'Task runs every week.
trigger.Id = "WeeklyTriggerId"
trigger.Enabled = True

'***********************************************************
' 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 Weekly Trigger", taskDefinition, 6, , , 3)

WScript.Echo "Task submitted."

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