如何从 Power Shell 设置任务调度程序的参数

How to set task scheduler's arguments from Power Shell

我想在任务调度程序中设置 "Add arguments"。 因为我们有很多 windows 个服务器,并且必须在每个服务器上设置添加参数。 我知道它必须管理任务调度程序 "tasks" 的命令,但我不知道如何只添加 "Add arguments"。 我想知道可以做我想做的事的命令(第一句)。 请问我关于这个问题你想知道什么。谢谢。

您需要修改任务操作,而不是任务本身:

# Retrieve task
$Task = Get-ScheduledTask -TaskName "myScheduledTask"

# Retrieve action, modify argument
$Task.Actions[0] = "new arguments string go here"

# Update task
Set-ScheduledTask $Task

通过示例学习(从打开的 elevated cmd window 复制粘贴;请注意 ^^> 是我的 admin 命令提示符):

^^> schtasks /query /TN SO_31969962 /V /FO LIST | findstr /R /C:"^Task To Run:" /C:"Start In"
Task To Run:                          D:\bat\SO969962.bat "1 st" second
Start In:                             D:\bat\SO\files

^^> schtasks /change /TN "\SO_31969962" /TR "D:\bat\SO969962.bat \"first\" second"
SUCCESS: The parameters of scheduled task "\SO_31969962" have been changed.

^^> schtasks /query /TN SO_31969962 /V /FO LIST | findstr /R /C:"^Task To Run:" /C:"Start In"
Task To Run:                          D:\bat\SO969962.bat "first" second
Start In:                             N/A

^^>

此处Task To Run: …行对应

不幸的是,schtasks.exe 无法指定“start-in”目录,如您在上面的示例中所见(阅读 this link、google 的整个线程 schtasks start in directory).

以下 PowerShell 代码片段更改 ArgumentsWorkingDirectory:

$Task = Get-ScheduledTask -TaskPath '\' -TaskName 'SO_31969962'
$Task.Actions[0].Arguments        = 'bubu "foo bar"'
$Task.Actions[0].WorkingDirectory = '"D:\bat\Unusual Names"'
Set-ScheduledTask -InputObject $Task | Out-Null

编辑:以下评论批处理脚本显示了how-to构造有效PowerShell one-line命令的可能方法(不需要 运行 现有的 .ps1 脚本):

@ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion

    rem related to D:\PShell\SO677069_ScheduledTask_Admin.ps1

    rem show current parameters of a task (before change) 
schtasks /query /TN "\SO_31969962" /V /FO LIST | findstr /R /C:"^Task To Run:" /C:"^Start In"

    rem set auxiliary variables (note properly escaped inner double quotes)
set "_taskGet=$Task = Get-ScheduledTask -TaskPath '\' -TaskName 'SO_31969962'"
set "_taskArg=$Task.Actions[0].Arguments = '\""foo bar\"" bubu'"
set "_taskDir=$Task.Actions[0].WorkingDirectory = '\""D:\odds and ends\""'"
set "_taskSet=Set-ScheduledTask -InputObject $Task"

    rem apply auxiliary variables (used merely to keep next line readable)
PowerShell -ExecutionPolicy Bypass -command "%_taskGet%;%_taskArg%;%_taskDir%;%_taskSet%"

    rem show current parameters of a task (after change)
schtasks /query /TN "\SO_31969962" /V /FO LIST | findstr /R /C:"^Task To Run:" /C:"^Start In"

结果(读powershell /?Get-Help 'about_powershell.exe' -ShowWindow; read also about_Execution_Policies):

^^> powershell -ExecutionPolicy Bypass -File "D:\PShell\SO677069_ScheduledTask_Admin.ps1"

^^> D:\bat\SO677069_ScheduledTask_Admin.bat
Task To Run:                          D:\bat\SO969962.bat bubu "foo bar"
Start In:                             "D:\bat\Unusual Names"

TaskPath                                       TaskName                          State
--------                                       --------                          -----
\                                              SO_31969962                       Disabled


Task To Run:                          D:\bat\SO969962.bat "foo bar" bubu
Start In:                             "D:\odds and ends"

^^>