Scripting Task Scheduler in Vista


I found the need to check several thousand machines on a regular basis to make sure BitLocker was installed and running.
I decided to run a script every time the machine booted up and for that I needed the Task Scheduler.

After some fumbeling around I gat this to work:

<------------------ Begin Script --------------------------->

Const TASK_ACTION_EXEC = 0 
Const TASK_CREATE = 2
Const TASK_Trigger_Boot = 8
Const TASK_RUNLEVEL_HIGHEST = 1
Const TASK_LOGON_PASSWORD = 1
Dim strUser, strPassword
strUser = "domainsvc-account"
strPassword = "Password"

Set objService = CreateObject("Schedule.Service")
objService.Connect()
Set objFolder = objService.GetFolder("")
Set objTaskDefinition = objService.NewTask(0)
Dim principal
Set principal = objTaskDefinition.Principal
' Set the logon type to TASK_LOGON_PASSWORD
principal.LogonType = 1
' RUNLEVEL = 1 Tasks will be run with the highest privileges.
' RUNLEVEL = 0 Tasks will be run with the least privileges (LUA).
principal.RunLevel = 1
Set colTasks = objTaskDefinition.Triggers
Set objTrigger = colTasks.Create(TASK_Trigger_Boot)
objTrigger.StartBoundary = "2009-05-27T08:00:00-00:00"
objTrigger.ExecutionTimeLimit = "PT5M" 'Five minutes
Set colActions = objTaskDefinition.Actions
Set objAction = colActions.Create(TASK_ACTION_EXEC)
objAction.ID = "BitLocker Check"
objAction.Path = "cscript.exe" ' <----- This could be powershell.exe
objAction.WorkingDirectory = "c:\scripts"
objAction.Arguments = "/nologo c:\scripts\chkBL.vbs" '<---Put your script here
'(or for PowerShell -noprofile -command "Path to .PS1 script" )
Set objInfo = objTaskDefinition.RegistrationInfo
objInfo.Author = "Author" <--- Put your name here
objInfo.Description = "Task that checks BitLocker status at every boot." '<--- Put a brief description here.
Set objSettings = objTaskDefinition.Settings
objSettings.Enabled = True
objSettings.Hidden = False
objSettings.StartWhenAvailable = True

objFolder.RegisterTaskDefinition "Check BitLocker Status", objTaskDefinition, TASK_CREATE, strUser, strPassword, 1

'<------------------ End Of Script --------------------------->

Have fun and may the force be with you.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s