Bagikan melalui


Contoh Pemicu Boot (Pembuatan Skrip)

Contoh pembuatan skrip ini menunjukkan cara membuat tugas yang dijadwalkan untuk menjalankan Notepad saat sistem di-boot. Tugas berisi pemicu boot yang menentukan batas awal dan waktu penundaan untuk tugas dimulai setelah sistem di-boot. Tugas ini juga berisi tindakan yang menentukan tugas untuk menjalankan Notepad. Tugas didaftarkan menggunakan akun Layanan Lokal sebagai konteks keamanan untuk menjalankan tugas.

Prosedur berikut menjelaskan cara menjadwalkan executable seperti Notepad untuk memulai saat sistem di-boot.

Untuk menjadwalkan Notepad untuk memulai saat sistem di-boot

  1. Buat objek TaskService . Objek ini memungkinkan Anda membuat tugas dalam folder yang ditentukan.
  2. Dapatkan folder tugas dan buat tugas. Gunakan metode TaskService.GetFolder untuk mendapatkan folder tempat tugas disimpan dan metode TaskService.NewTask untuk membuat objek TaskDefinition yang mewakili tugas.
  3. Tentukan informasi tentang tugas menggunakan objek TaskDefinition . Gunakan properti TaskDefinition.Settings untuk menentukan pengaturan yang menentukan bagaimana layanan Penjadwal Tugas melakukan tugas dan properti TaskDefinition.RegistrationInfo untuk menentukan informasi yang menjelaskan tugas.
  4. Buat pemicu masuk menggunakan properti TaskDefinition.Triggers . Properti ini menyediakan akses ke objek TriggerCollection . Gunakan metode TriggerCollection.Create (menentukan jenis pemicu yang ingin Anda buat) untuk membuat pemicu boot. Saat Anda membuat pemicu, atur properti StartBoundary dan EndBoundary pemicu untuk mengaktifkan dan menonaktifkan pemicu. Anda juga dapat menentukan nilai untuk properti Penundaan pemicu boot.
  5. Buat tindakan untuk tugas yang akan dijalankan dengan menggunakan properti TaskDefinition.Actions . Properti ini menyediakan akses ke objek ActionCollection . Gunakan metode ActionCollection.Create untuk menentukan jenis tindakan yang ingin Anda buat. Contoh ini menggunakan objek ExecAction , yang mewakili tindakan yang memulai executable.
  6. Daftarkan tugas menggunakan metode TaskFolder.RegisterTaskDefinition . Tugas didaftarkan menggunakan akun Layanan Lokal sebagai konteks keamanan untuk menjalankan tugas.

Contoh VBScript berikut menunjukkan cara menjadwalkan tugas untuk menjalankan Notepad 30 detik setelah sistem di-boot.

'---------------------------------------------------------
' This sample schedules a task to start notepad.exe 30 seconds after
' the system is booted.
'---------------------------------------------------------

' A constant that specifies a boot trigger.
const TriggerTypeBoot = 8
' 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 = "Task will execute Notepad when " & _
    "the computer is booted."
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 boot trigger.
Dim triggers
Set triggers = taskDefinition.Triggers

Dim trigger
Set trigger = triggers.Create(TriggerTypeBoot)

' Trigger variables that define when the trigger is active.
Dim startTime, endTime
startTime = "2006-05-02T10:49:02"
endTime = "2006-05-02T10:52:02"

WScript.Echo "startTime :" & startTime
WScript.Echo "endTime :" & endTime

trigger.StartBoundary = startTime
trigger.EndBoundary = endTime
trigger.ExecutionTimeLimit = "PT5M"    ' Five minutes
trigger.Id = "BootTriggerId"
trigger.Delay = "PT30S"                ' 30 Seconds   

'***********************************************************
' 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.
const createOrUpdateTask = 6
call rootFolder.RegisterTaskDefinition( _
    "Test Boot Trigger", taskDefinition, createOrUpdateTask, _
    "Local Service", , 5)

WScript.Echo "Task submitted."

Menggunakan Penjadwal Tugas