Creating a scheduled task with multiple actions using command line in Windows server 2008

I had the need to create a scheduled task on a Windows Server 2008 server that executed mutiple actions.   For those who haven’t worked with Windows Server 2008 new task scheduler, there are links listed below to help with that.  My post assumes you know about task scheduler and want to go one step further and automate creating using the command prompt.


To get started, on a single machine.  Create the scheduled task you want to run, this would include all the necessary actions. 



Create multiple actions.



Export the scheduled task



Here is the output from the export step.  Notice at the bottom it has the EXEC command.   


<?xml version=”1.0″ encoding=”UTF-16″?>
<Task version=”1.2″ xmlns=”http://schemas.microsoft.com/windows/2004/02/mit/task“>
  <RegistrationInfo>
    <Date>2009-03-20T05:18:33.8972515</Date>
    <Author>DomainNameUserName</Author>
  </RegistrationInfo>
  <Triggers>
    <CalendarTrigger>
      <StartBoundary>2009-03-20T05:16:04.8833515</StartBoundary>
      <Enabled>true</Enabled>
      <ScheduleByDay>
        <DaysInterval>1</DaysInterval>
      </ScheduleByDay>
    </CalendarTrigger>
  </Triggers>
  <Principals>
    <Principal id=”Author”>
      <UserId>DomainNameUserName</UserId>
      <LogonType>Password</LogonType>
      <RunLevel>LeastPrivilege</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <IdleSettings>
      <Duration>PT10M</Duration>
      <WaitTimeout>PT1H</WaitTimeout>
      <StopOnIdleEnd>true</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>false</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>P3D</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context=”Author”>
    <Exec>
      <Command>c:windowssystem32notepad.exe</Command>
    </Exec>
    <Exec>
      <Command>C:WindowsSystem32WindowsPowerShellv1.0powershell.exe</Command>
      <Arguments>\FileServerFileSharetest.ps1</Arguments>
    </Exec>
  </Actions>
</Task>


To automate creation, run from the command prompt.  For my testing, I created the task under one name and when I used the command prompt, I gave the TN (task name) property something else so I could have both jobs on my test box.


C:WindowsSystem32schtasks /create /tn “RunSomeTaskWith2Actions” /RU DomainNameUsername /RP Password1 /xml \FileServerFileShareExportedFile.xml


PS – To create the task on several machines, you could use a powershell script or something to call WMI (win32_process) to create everywhere.  With a little scripting and this, the task could be automated.  I hope this is helpful. 


 Additional resources




  • http://www.windowsnetworking.com/articles_tutorials/Working-Windows-Server-2008-Task-Scheduler-Part1.html


  • http://www.windowsnetworking.com/articles_tutorials/Working-Windows-Server-2008-Task-Scheduler-Part2.html






  • http://blogs.technet.com/askperf/archive/2008/06/24/task-scheduler-changes-in-windows-vista-and-windows-server-2008-part-one.aspx


  • http://blogs.technet.com/askperf/archive/2008/10/10/task-scheduler-changes-in-windows-vista-and-windows-server-2008-part-two.aspx


  • http://blogs.technet.com/askperf/archive/2009/03/17/task-scheduler-changes-in-windows-vista-and-windows-server-2008-part-three.aspx

Cheers,


Steve Schofield
Microsoft MVP – IIS