PowerShell:更新计划任务用户名和密码?

标签 powershell scheduled-tasks

我正在寻找一种方法来更新计算机上现有计划任务的凭据。

  • 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/

相关文章:

powershell - 将文件夹结构复制到不存在的位置

ruby-on-rails - 如何在 Heroku 上安排选定日期的工作?

powershell - 为什么通过PowerShell使用其他用户创建的每周任务失败并出现错误0x41306

jakarta-ee - EJB @Schedule 问题

c# - Windows Server Task Scheduler 需要将文件放在 System32 文件夹中吗?

windows - 文件夹上的 Powershell Remove-Item 仅在通过 Ansible 运行时失败

python - 如何使用新时间戳重写文件中的某些行

java - 通过从 Java 执行 PowerShell 脚本来终止服务

azure - PowerShell 脚本用于在 azure 容器注册表上保留最新 10 个镜像,其余的需要删除

windows - 如何找到正在运行的计划任务的进程ID?