Esempio di trigger giornaliero (scripting)

Questo esempio di script illustra come creare un'attività che esegue blocco note alle 8:00 ogni giorno. L'attività contiene un trigger giornaliero che specifica un limite di inizio per attivare il trigger e per specificare l'ora di esecuzione dell'attività, un intervallo di trigger per specificare che l'attività viene eseguita ogni giorno e un limite finale per disattivare il trigger. L'esempio mostra anche come impostare un modello di ripetizione per il trigger per ripetere l'attività. L'attività contiene anche un'azione eseguibile che esegue Blocco note.

La procedura seguente descrive come pianificare un'attività per avviare un eseguibile alle 8:00 ogni giorno. Questi passaggi corrispondono ai commenti di codice inclusi nel codice di esempio.

Per pianificare l'inizio del Blocco note alle 8:00 ogni giorno

  1. Creare un oggetto TaskService . Questo oggetto consente di creare l'attività in una cartella specificata.
  2. Ottenere una cartella attività e creare un'attività. Utilizzare il metodo TaskService.GetFolder per ottenere la cartella in cui è archiviata l'attività e il metodo TaskService.NewTask per creare l'oggetto TaskDefinition che rappresenta l'attività.
  3. Definire informazioni sull'attività utilizzando l'oggetto TaskDefinition . Utilizzare la proprietà TaskDefinition.Settings per definire le impostazioni che determinano il modo in cui il servizio Utilità di pianificazione esegue l'attività e la proprietà TaskDefinition.RegistrationInfo per definire le informazioni che descrivono l'attività.
  4. Creare un trigger giornaliero usando la proprietà TaskDefinition.Triggers . Questa proprietà consente di accedere all'oggetto TriggerCollection utilizzato per creare il trigger. Utilizzare il metodo TriggerCollection.Create (specificando il tipo di trigger da creare) per creare un trigger giornaliero. Quando si crea il trigger, impostare il limite di inizio per attivare il trigger e specificare l'ora di esecuzione dell'attività, l'intervallo tra i giorni e il limite finale per disattivare il trigger. L'esempio seguente illustra come impostare un modello di ripetizione per il trigger per ripetere l'attività.
  5. Creare un'azione per l'attività da eseguire usando la proprietà TaskDefinition.Actions . Questa proprietà consente di accedere all'oggetto ActionCollection utilizzato per creare l'azione. Utilizzare il metodo ActionCollection.Create per specificare il tipo di azione da creare. In questo esempio viene utilizzato un oggetto ExecAction , che rappresenta un'azione che esegue un'operazione della riga di comando.
  6. Registrare l'attività usando il metodo TaskFolder.RegisterTaskDefinition . Per questo esempio, l'attività avvierà blocco note alle 8:00 ogni giorno.

Nell'esempio di VBScript seguente viene illustrato come pianificare un'attività per l'esecuzione del Blocco note ogni giorno alle 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."

Uso dell'Utilità di pianificazione