Reputation: 145269
I tried a number of variations of Microsoft's example. None worked. The following code is what it currently looks like, and (see below) that doesn't work:
option explicit
'********************************************************
' Create the TaskService object.
Dim service
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)
'----------------- principal
' taskDefinition.principal.LogonType = 3
' taskDefinition.principal.UserId = "S-1-5-21-2764009396-4153354299-2297777058-1001"
' taskDefinition.principal.RunLevel = 0 ' Least privilege
' taskDefinition.principal.GroupId = "Builtin\Administrators"
'********************************************************
' 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 a " & _
"specified user logs on."
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 logon trigger.
' A constant that specifies a logon trigger.
const TriggerTypeLogon = 9
Dim triggers
Set triggers = taskDefinition.Triggers
Dim trigger
Set trigger = triggers.Create(TriggerTypeLogon)
' Trigger variables that define when the trigger is active.
Dim startTime, endTime
startTime = "2006-05-02T10:49:02"
endTime = "2046-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 = "LogonTriggerId"
trigger.UserId = "alfp" ' "alfswindowspc10\alfp" ' Must be a valid user account
trigger.Enabled = True
'***********************************************************
' Create the action for the task to execute.
' A constant that specifies an executable action.
const ActionTypeExecutable = 0
' 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 Logon Trigger", taskDefinition, createOrUpdateTask, _
"Builtin\Administrators", , 4)
WScript.Echo "Task submitted."
All attempts so far have crashed on the rootFOlder.RegisterTaskDefinition
call at the end, apparently claiming there's something wrong with the user id, or alternatively with the group id.
[C:\my\tezt]
> cscript //e:vbscript //nologo example_create_task.vbs
Task definition created. About to submit the task...
C:\my\tezt\example_create_task.vbs(88, 5) (null): (42,4):GroupId:
[C:\my\tezt]
>_
It does not seem to matter whether I run it in normal or elevated mode.
Scheduling a logon task via schtasks
command works generally for XML task definition, and from elevated mode for a task specified via options.
Upvotes: 0
Views: 1348
Reputation: 145269
It turned out that the magic number 4 in the MS code was TASK_LOGON_GROUP
. I changed that to 3, TASK_LOGON_INTERACTIVE_TOKEN
, and also removed the "Builtin\Administrators"
user id override, and now it works.
Upvotes: 2