windows - How to schedule a logon task via VBScript -
i tried number of variations of microsoft's example. none worked. following code looks like, , (see below) doesn't work:
option explicit '******************************************************** ' create taskservice object. dim service set service = createobject( "schedule.service" ) call service.connect() '******************************************************** ' folder create task definition in. dim rootfolder set rootfolder = service.getfolder("\") ' taskdefinition variable taskdefinition object. dim taskdefinition ' flags parameter 0 because 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 task. ' set registration info task ' creating registrationinfo object. dim reginfo set reginfo = taskdefinition.registrationinfo reginfo.description = "task execute notepad when " & _ "specified user logs on." reginfo.author = "author name" ' set task setting info task scheduler ' creating tasksettings object. dim settings set settings = taskdefinition.settings settings.startwhenavailable = true '******************************************************** ' create logon trigger. ' constant specifies logon trigger. const triggertypelogon = 9 dim triggers set triggers = taskdefinition.triggers dim trigger set trigger = triggers.create(triggertypelogon) ' trigger variables define when trigger 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" ' 5 minutes trigger.id = "logontriggerid" trigger.userid = "alfp" ' "alfswindowspc10\alfp" ' must valid user account trigger.enabled = true '*********************************************************** ' create action task execute. ' constant specifies executable action. const actiontypeexecutable = 0 ' add action task. action executes notepad. dim action set action = taskdefinition.actions.create( actiontypeexecutable ) action.path = "c:\windows\system32\notepad.exe" wscript.echo "task definition created. submit task..." '*********************************************************** ' register (create) task. const createorupdatetask = 6 call rootfolder.registertaskdefinition( _ "test logon trigger", taskdefinition, createorupdatetask, _ "builtin\administrators", , 4) wscript.echo "task submitted."
all attempts far have crashed on rootfolder.registertaskdefinition
call @ end, apparently claiming there's wrong user id, or alternatively group id.
[c:\my\tezt] > cscript //e:vbscript //nologo example_create_task.vbs task definition created. submit task... c:\my\tezt\example_create_task.vbs(88, 5) (null): (42,4):groupid: [c:\my\tezt] >_
it not seem matter whether run in normal or elevated mode.
scheduling logon task via schtasks
command works xml task definition, , elevated mode task specified via options.
it turned out magic number 4 in ms code task_logon_group
. changed 3, task_logon_interactive_token
, , removed "builtin\administrators"
user id override, , works.
Comments
Post a Comment