Esempio di trigger di registrazione (scripting)

In questo esempio di script viene illustrato come creare un'attività pianificata per l'esecuzione del Blocco note quando viene registrata un'attività. L'attività contiene un trigger di registrazione che specifica un limite di inizio e un limite finale per l'attività. Il limite iniziale specifica quando il trigger viene attivato. L'attività contiene anche un'azione che specifica l'attività da eseguire blocco note.

Nota

Quando un'attività con un trigger di registrazione viene aggiornata, l'attività verrà eseguita dopo l'aggiornamento.

 

La procedura seguente descrive come pianificare un eseguibile, ad esempio blocco note, per avviare quando viene registrata un'attività.

Per pianificare l'avvio del Blocco note quando viene registrata un'attività

  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 di registrazione usando la proprietà TaskDefinition.Triggers . Questa proprietà consente l'accesso all'oggetto TriggerCollection . Utilizzare il metodo TriggerCollection.Create (specificando il tipo di trigger da creare) per creare un trigger di registrazione.
  5. Creare un'azione per l'attività da eseguire usando la proprietà TaskDefinition.Actions . Questa proprietà fornisce l'accesso all'oggetto ActionCollection . 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 avvia un eseguibile.
  6. Registrare l'attività usando il metodo TaskFolder.RegisterTaskDefinition .

Nell'esempio di VBScript seguente viene illustrato come creare un'attività che pianifica l'esecuzione del Blocco note quando viene registrata l'attività.

'---------------------------------------------------------
' This sample schedules a task to start notepad.exe when
' the task is registered.
'---------------------------------------------------------

' A constant that specifies a registration trigger.
const TriggerTypeRegistration = 7
' A constant that specifies an executable action.
const ActionTypeExecutable = 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 when the task is registered."
regInfo.Author = "Author Name"

' Set the task setting info for the Task Scheduler by
' creating a TaskSettings object.
Dim settings
Set settings = taskDefinition.Settings
settings.StartWhenAvailable = True

'********************************************************
' Create a registration trigger.
Dim triggers
Set triggers = taskDefinition.Triggers

Dim trigger
Set trigger = triggers.Create(TriggerTypeRegistration)

trigger.ExecutionTimeLimit = "PT5M"    'Five minutes
trigger.Id = "RegistrationTriggerId"   

'***********************************************************
' Create the action for the task to execute.

' Add an action to the task. The action executes Notepad.
Dim Action
Set Action = taskDefinition.Actions.Create( ActionTypeExecutable )
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 Registration Trigger", taskDefinition, 6, , , 3)

WScript.Echo "Task submitted."

Uso dell'Utilità di pianificazione