Esempio di trigger settimanale (scripting)

Questo esempio di scripting illustra come creare un'attività che esegue il Blocco note alle 8:00 di lunedì di ogni settimana. L'attività contiene un trigger giornaliero che specifica quando l'attività viene eseguita e un'azione eseguibile che esegue Il blocco note.

La procedura seguente descrive come pianificare un'attività per avviare un eseguibile alle 8:00 di lunedì della settimana.

Per pianificare il blocco note per iniziare alle 8:00 di lunedì di ogni settimana

  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à usando l'oggetto TaskDefinition . Utilizzare la proprietà TaskDefinition.Settings per definire le impostazioni che determinano come 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 settimanale usando la proprietà TaskDefinition.Triggers . Questa proprietà fornisce l'accesso all'oggetto TriggerCollection usato per creare il trigger.

    Usare il metodo TriggerCollection.Create (specificando il tipo di trigger che si vuole creare) per creare un trigger settimanale.

    Impostare la proprietà WeeklyTrigger.StartBoundary per specificare quando il trigger viene attivato e l'ora del giorno in cui viene eseguita l'attività. In questo esempio il trigger viene attivato il 1° gennaio 2005 e l'attività viene eseguita alle 8:00.

    Impostare la proprietà WeeklyTrigger.EndBoundaryper specificare quando il trigger viene disattivato. In questo esempio il trigger viene disattivato il 1° gennaio 2015.

    Impostare la proprietà WeeklyTrigger.DaysOfWeek per specificare i giorni della settimana in cui viene eseguita l'attività. In questo esempio l'attività viene eseguita il lunedì.

    Impostare la proprietà WeeklyTrigger.WeeksIntervalper specificare l'intervallo tra le settimane della pianificazione. In questo esempio l'attività viene eseguita ogni settimana.

  5. Creare un'azione per l'attività da eseguire usando la proprietà TaskDefinition.Actions . Questa proprietà fornisce l'accesso all'oggetto ActionCollection usato per creare l'azione. Usare il metodo ActionCollection.Create per specificare il tipo di azione da creare. In questo esempio viene usato un oggetto ExecAction , che rappresenta un'azione che esegue un'operazione da riga di comando.

  6. Registrare l'attività usando il metodo TaskFolder.RegisterTaskDefinition . Per questo esempio, l'attività inizierà il Blocco note alle 8:00 il lunedì di ogni settimana.

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

Uso dell'utilità di pianificazione delle attività