我正在寻找一种方法来更新计算机上现有计划任务的凭据。
- Schtasks 不适用于 AT 创建的计划任务
- Win32_ScheduledJob 仅适用于 AT 创建的作业
- Schedule.Service COM 对象 - 不确定
看来我可以使用 RegisterTask 和 RegisterTaskDefinition 来创建计划任务,但我不清楚是否可以使用这些方法更新现有凭据。请指教。谢谢。
最佳答案
我建议查看托管 TaskScheduler API,它是 TaskScheduler COM API 的 .NET 包装器。它是 CodePlex 上的一个开源项目。
http://taskscheduler.codeplex.com/
项目作者对于更新密码有这样的说法:
If you are trying to create a task using the credentials of the current user and you only want it to run when that user is logged in, you need to call the RegisterTaskDefinition method as in the end of the Complex example with the InteractiveToken parameter. If you need to create as another specific user, then use that same method, but supply the username, password, and set the TaskLogonType to InteractiveTokenOrPassword or Password. There are some triggers that are specific to a user, like the LogonTrigger where you can also supply a user credential.
RegisterTaskDefinition
的适当重载在 TaskFolder.cs 中定义。
http://taskscheduler.codeplex.com/SourceControl/changeset/view/75611#19440
public Task RegisterTaskDefinition(string Path, TaskDefinition definition
, TaskCreation createType, string UserId, string password = null
, TaskLogonType LogonType = TaskLogonType.S4U, string sddl = null)
关于PowerShell:更新计划任务用户名和密码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9488016/