此脚本示例演示如何创建每天上午 8:00 记事本运行的任务。 该任务包含一个每日触发器,该触发器指定要激活触发器的开始边界,并指定任务运行的一天时间、指定每天运行任务的触发器间隔,以及用于停用触发器的结束边界。 该示例还演示如何为触发器设置重复模式以重复任务。 该任务还包含运行记事本的可执行操作。

以下过程介绍如何计划任务,以每天上午 8:00 启动可执行文件。 (这些步骤对应于示例 code.) 中包含的代码注释

安排记事本从每天上午 8:00 开始

  • 创建 TaskService 对象。 此对象允许在指定文件夹中创建任务。
  • 获取任务文件夹并创建任务。 使用 TaskService.GetFolder 方法获取存储任务的文件夹,使用 TaskService.NewTask 方法创建表示任务的 TaskDefinition 对象。
  • 使用 TaskDefinition 对象定义有关该任务的信息。 使用 TaskDefinition.设置 属性定义确定任务计划程序服务执行任务的方式和 TaskDefinition.RegistrationInfo 属性的设置,以定义描述任务的信息。
  • 使用 TaskDefinition.Triggers 属性创建每日触发器。 此属性提供对用于创建触发器的 TriggerCollection 对象的访问。 使用 TriggerCollection.Create 方法 (指定要创建) 创建每日触发器的触发器类型。 创建触发器时,设置开始边界以激活触发器,并指定任务运行的一天时间、天数之间的间隔和结束边界以停用触发器。 下面的示例演示如何为触发器设置重复模式以重复任务。
  • 使用 TaskDefinition.Actions 属性为要执行的任务创建操作。 此属性提供对用于创建操作的 ActionCollection 对象的访问。 使用 ActionCollection.Create 方法指定要创建的操作的类型。 此示例使用 ExecAction 对象,该对象表示执行命令行操作的操作。
  • 使用 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."
    

    使用任务计划程序