我可以从命令行创建一个在登录时运行的计划任务,无需管理员权限或用户输入密码来设置任务。但是我必须使用 xml 文件来执行此操作。这是一个示例 xml,其中“Domain\User”部分必须在运行时替换为当前用户的域和名称:
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Author>Domain\User</Author>
</RegistrationInfo>
<Triggers>
<LogonTrigger>
<Enabled>true</Enabled>
<UserId>Domain\User</UserId>
</LogonTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<UserId>Domain\User</UserId>
<LogonType>InteractiveToken</LogonType>
</Principal>
</Principals>
<Settings>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
</Settings>
<Actions Context="Author">
<Exec>
<Command>"C:\Windows\notepad.exe"</Command>
<Arguments>/logon</Arguments>
</Exec>
</Actions>
</Task>
我可以使用 xml 使用命令创建任务:
schtasks /create /TN "Test Logon Task" /XML task.xml
这很好用,但我想在 vbs 中做同样的事情,而不需要为用户编写然后加载 xml 文件。我一直在尝试调整 example from MSDN但到目前为止,我无法在没有管理员权限或提示输入用户密码的情况下添加任务。
有人知道怎么做吗?
最佳答案
可能是 Schtasks.exe - 来自 Microsoft 文档
Permissions for schtasks
You must have permission to run the command. Any user can schedule
a task on the local computer, and they can view and change the tasks
that they scheduled. Members of the Administrators group can schedule,
view, and change all tasks on the local computer.
其中一个参数 (/sc onlogon
) 允许定义将在登录时运行的任务。
似乎有可能是答案,但我还没有尝试过。
编辑
由于 schtasks 被证明不是答案,我已经从 Microsoft 获取原始代码并适应工作(我希望,对我有用)
'---------------------------------------------------------
' This sample schedules a task to start notepad.exe when a user logs on.
'---------------------------------------------------------
' A constant that specifies a logon trigger.
const TriggerTypeLogon = 9
' A constant that specifies an executable action.
const ActionTypeExecutable = 0
const TASK_LOGON_INTERACTIVE_TOKEN = 3
'********************************************************
' 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 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.
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 = "2013-10-22T10:49:02"
endTime = "2013-10-30T10: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 = "MyDomainOrComputer\testUser" ' Must be a valid user account
'***********************************************************
' 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..."
'***********************************************************
' Deal with the password problems
taskDefinition.Principal.UserId = "MyDomainOrComputer\testUser"
taskDefinition.Principal.LogonType = TASK_LOGON_INTERACTIVE_TOKEN
'***********************************************************
' Register (create) the task.
const createOrUpdateTask = 6
call rootFolder.RegisterTaskDefinition( _
"Test Logon Trigger", taskDefinition, createOrUpdateTask, _
Empty , Empty, TASK_LOGON_INTERACTIVE_TOKEN)
WScript.Echo "Task submitted."
更改了 RegisterTaskDefinition 调用以不传递任务的凭据,并配置了 taskDefinition 的 del Principal
对象属性
使用非管理员用户注册的任务,没有密码提示。
关于windows - 如何使用没有管理员权限的 vbs 将任务设置为在登录时以当前用户身份运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19522094/