次の方法で共有


タイム トリガーの例 (スクリプト)

このスクリプトの例では、メモ帳を特定の時刻に実行するタスクを作成する方法を示します。 タスクには、タスクをアクティブ化するための開始境界、メモ帳を実行する実行可能なアクション、タスクを非アクティブ化する終了境界を指定する時間ベースのトリガーが含まれています。

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

メモ帳を特定の時刻に開始するようにスケジュール

  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 メソッドを使用してタスクを登録します。 この例では、タスクは現在の時刻と 30 秒でメモ帳を開始します。

次の VBScript の例は、タスクが登録されてから 30 秒後にメモ帳を実行するようにタスクをスケジュールする方法を示しています。

'------------------------------------------------------------------
' This sample schedules a task to start notepad.exe 30 seconds
' from the time the task is registered.
'------------------------------------------------------------------

' A constant that specifies a time-based trigger.
const TriggerTypeTime = 1
' 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 a certain time"
regInfo.Author = "Author Name"

'********************************************************
' Set the principal for the task
Dim principal
Set principal = taskDefinition.Principal

' Set the logon type to interactive logon
principal.LogonType = 3


' 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 time-based trigger.
Dim triggers
Set triggers = taskDefinition.Triggers

Dim trigger
Set trigger = triggers.Create(TriggerTypeTime)

' Trigger variables that define when the trigger is active.
Dim startTime, endTime

Dim time
time = DateAdd("s", 30, Now)  'start time = 30 seconds from now
startTime = XmlTime(time)

time = DateAdd("n", 5, Now) 'end time = 5 minutes from now
endTime = XmlTime(time)

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

trigger.StartBoundary = startTime
trigger.EndBoundary = endTime
trigger.ExecutionTimeLimit = "PT5M"    'Five minutes
trigger.Id = "TimeTriggerId"
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 TimeTrigger", taskDefinition, 6, , , 3)

WScript.Echo "Task submitted."



'------------------------------------------------------------------
' Used to get the time for the trigger 
' startBoundary and endBoundary.
' Return the time in the correct format: 
' YYYY-MM-DDTHH:MM:SS. 
'------------------------------------------------------------------
Function XmlTime(t)
    Dim cSecond, cMinute, CHour, cDay, cMonth, cYear
    Dim tTime, tDate

    cSecond = "0" & Second(t)
    cMinute = "0" & Minute(t)
    cHour = "0" & Hour(t)
    cDay = "0" & Day(t)
    cMonth = "0" & Month(t)
    cYear = Year(t)

    tTime = Right(cHour, 2) & ":" & Right(cMinute, 2) & _
        ":" & Right(cSecond, 2)
    tDate = cYear & "-" & Right(cMonth, 2) & "-" & Right(cDay, 2)
    XmlTime = tDate & "T" & tTime 
End Function

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