Search code examples
powershellwindows-server-2012windows-server-2012-r2

How to set task scheduler's arguments from Power Shell


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.


Solution

  • 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

    edit action

    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"
    
    ^^>