共用方式為


每日觸發程式範例 (文稿)

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

下列程序說明如何排程工作每天上午 8:00 啟動可執行檔。 (這些步驟會對應至範例程式代碼中包含的程式代碼批注。

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

  1. 建立 TaskService 物件。 這個物件可讓您在指定的資料夾中建立工作。
  2. 取得工作資料夾並建立工作。 使用 TaskService.GetFolder 方法來取得工作儲存所在的資料夾,以及 TaskService.NewTask 方法來建立代表工作的 TaskDefinition 物件。
  3. 使用 TaskDefinition 物件定義工作的相關信息。 使用 TaskDefinition.Settings 屬性來定義可決定 Task Scheduler 服務如何執行工作的設定,以及 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."

使用工作排程器