如何从Power Shell

时间:2017-01-16 13:03:54

标签: powershell windows-server-2012 windows-server-2012-r2

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

2 个答案:

答案 0 :(得分:0)

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

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

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

# Update task
Set-ScheduledTask $Task

答案 1 :(得分:0)

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

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

^^> schtasks /change /TN "\SO_31969962" /TR "D:\bat\SO\31969962.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\SO\31969962.bat "first" second
Start In:                             N/A

^^>

此处Task To Run: …行对应

edit action

不幸的是,schtasks.exe无法指定“开始”目录,正如您在上面的示例中看到的那样(阅读this link的整个帖子,google for 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

编辑:在评论批处理脚本后,显示了如何构建有效的PowerShell单行命令的可能方法(无需运行现有的.ps1脚本):

@ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion

    rem related to D:\PShell\SO\41677069_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;另请阅读about_Execution_Policies):

^^> powershell -ExecutionPolicy Bypass -File "D:\PShell\SO\41677069_ScheduledTask_Admin.ps1"

^^> D:\bat\SO\41677069_ScheduledTask_Admin.bat
Task To Run:                          D:\bat\SO\31969962.bat bubu "foo bar"
Start In:                             "D:\bat\Unusual Names"

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


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

^^>