如何通过VBScript安排登录任务

时间:2017-09-12 06:56:27

标签: windows winapi vbscript scheduled-tasks schtasks

我尝试了Microsoft's example的多种变体。没有用。以下代码是目前的样子,并且(见下文)不起作用:

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."

到目前为止,所有尝试都在最后的rootFOlder.RegisterTaskDefinition电话上崩溃,显然声称用户ID存在问题,或者使用组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]
>_

我是否以正常模式或高架模式运行似乎并不重要。

通过schtasks命令调度登录任务通常用于XML任务定义,而从升级模式用于通过选项指定的任务。

1 个答案:

答案 0 :(得分:2)

事实证明,MS代码中的幻数4是TASK_LOGON_GROUP。我将其更改为3 TASK_LOGON_INTERACTIVE_TOKEN,并删除了"Builtin\Administrators"用户ID覆盖,现在它可以正常工作。

相关问题