此腳本範例示範如何建立工作,以每周星期一上午 8:00 執行記事本。 工作包含每日觸發程式,指定工作何時執行,以及執行記事本的可執行動作。
下列程序說明如何排程工作,以在每周星期一上午 8:00 開始可執行檔。
將記事本排程為每週一上午 8:00 開始
建立 TaskService 物件。 這個物件可讓您在指定的資料夾中建立工作。
取得工作資料夾並建立工作。 使用 TaskService.GetFolder 方法來取得工作儲存所在的資料夾,以及 TaskService.NewTask 方法來建立代表工作的 TaskDefinition 物件。
使用 TaskDefinition 物件定義工作的相關信息。 使用 TaskDefinition.Settings 屬性來定義可決定 Task Scheduler 服務如何執行工作的設定,以及 TaskDefinition.RegistrationInfo 屬性來定義描述工作的資訊。
使用 TaskDefinition.Triggers 屬性建立每周觸發程式。 這個屬性可讓您存取用來建立觸發程式的 TriggerCollection 物件。
使用 TriggerCollection.Create 方法(指定要建立的觸發程式類型)來建立每周觸發程式。
設定 WeeklyTrigger.StartBoundary 屬性,以指定觸發程式啟動的時間,以及執行工作的時間。 在此範例中,觸發程式會在 2005 年 1 月 1 日啟動,工作會在上午 8:00 執行。
設定 WeeklyTrigger.EndBoundary屬性,以指定停用觸發程式時。 在此範例中,觸發程式會在 2015 年 1 月 1 日停用。
設定 WeeklyTrigger.DaysOfWeek 屬性,以指定工作執行所在的星期幾。 在此範例中,工作會在星期一執行。
設定 WeeklyTrigger.WeeksInterval屬性,以指定排程中周之間的間隔。 在此範例中,工作會每周執行。
使用 taskDefinition.Actions 屬性,建立要執行之工作的動作。 這個屬性可讓您存取用來建立動作的 ActionCollection 物件。 使用 ActionCollection.Create 方法來指定您要建立的動作類型。 這個範例會使用 ExecAction 物件,代表執行命令行作業的動作。
使用 TaskFolder.RegisterTaskDefinition 方法來註冊工作。 在此範例中,工作會在每周星期一上午 8:00 開始記事本。
下列 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."
相關主題