I want to set "Add arguments" in the task scheduler. Because we have a lot of windows servers and have to set add arguments on each server. I know it has to manage command for task scheduler "tasks" but I don't know how to add only "Add arguments". I want to know the command that can do what I want to do (first sentence). Please ask me anything what you want to know about this problem. Thank you.
Asked
Active
Viewed 5,059 times
-3
-
1Task scheduler administration is better on-topic in ServerFault. That being said, take a look at scheduler's [XML](https://msdn.microsoft.com/en-us/library/windows/desktop/aa383609(v=vs.85).aspx) schema. – vonPryz Jan 16 '17 at 13:07
-
Please give us a more detailed description of the problem. Thx – Moerwald Jan 16 '17 at 13:17
-
@vonPryz You could have provided [a link to Server Fault](http://serverfault.com/). New users haven't necessarily heard about it. – Donald Duck Jan 16 '17 at 13:19
-
Please provide further details of the issue – Ranadip Dutta Jan 16 '17 at 14:02
-
1In PowerShell, I'd use cmdlets from `ScheduledTasks` and `PSScheduledJob` modules rather than `schtasks.exe`. – JosefZ Jan 16 '17 at 14:05
-
I don't want to set task's arguments manually because it takes long time so I need to make bat file.However I am not sure about how to set task's arguments from command. Also I confuse about PowerShell and command prompt.. – tekun Jan 16 '17 at 14:30
2 Answers
1
You'll need to modify the task action, not the task itself:
# Retrieve task
$Task = Get-ScheduledTask -TaskName "myScheduledTask"
# Retrieve action, modify argument
$Task.Actions[0] = "new arguments string go here"
# Update task
Set-ScheduledTask $Task

Mathias R. Jessen
- 157,619
- 12
- 148
- 206
1
Learn by example (copied & pasted from an open elevated cmd
window; note that ^^>
is my admin command prompt):
^^> 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
^^>
Here the Task To Run: …
line corresponds to
Unfortunately, schtasks.exe
fails in specifying “start-in” directory as you can see in above example (read entire thread of this link, google for schtasks start in directory
).
Following PowerShell code snippet changes both Arguments
and WorkingDirectory
:
$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
Edit: following commented batch script shows possible approach of how-to construct a valid PowerShell one-line command (no need to run an existing .ps1
script):
@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"
Result (read powershell /?
or Get-Help 'about_powershell.exe' -ShowWindow
; read also 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"
^^>
-
-
I will try to use power shell cuz start in parameter is not work in command prompt. – tekun Jan 17 '17 at 14:20
-
-