共用方式為


每日觸發程式範例 (腳本)

此腳本範例示範如何建立每天上午 8:00 執行記事本的工作。 此工作包含一個每日觸發程式,指定要啟動觸發程式的開始界限,以及指定工作執行的當日時間、指定工作每天執行的觸發間隔,以及停用觸發程式的結束界限。 此範例也會示範如何設定重複模式,讓觸發程式重複工作。 此工作也包含執行記事本的可執行動作。

下列程式描述如何排程工作每天上午 8:00 啟動可執行檔。 (這些步驟會對應至範例 code.) 中包含的程式碼批註

將記事本排程為每天上午 8:00 開始

  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 方法註冊工作。 在此範例中,工作會每天上午 8:00 開始記事本。

下列 VBScript 範例示範如何排程工作每天上午 8:00 執行記事本。

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

使用工作排程器