Create schedule task from xml file by batch file

I have a schedule task in my test machine. Test machine is win 7. I export the task in xml and I am trying to create a new similar schedule task as i will have to create same schedule task on more than 500 machine.

I am planning to create a batch file for schedule task. As most machines are in same domain. So, i am planning to use service account for authentication.

Below is the xml file. This is working fine at the moment from task scheduler.

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.3" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2016-01-26T19:26:36.6011086</Date>
    <Author>test\404</Author>
    <Description>Testing Tasks</Description>
  </RegistrationInfo>
  <Triggers>
    <CalendarTrigger>
      <Repetition>
        <Interval>PT1H</Interval>
        <Duration>P1D</Duration>
        <StopAtDurationEnd>false</StopAtDurationEnd>
      </Repetition>
      <StartBoundary>2016-01-26T19:25:42.7621086</StartBoundary>
      <EndBoundary>2017-01-26T19:25:43.0101086</EndBoundary>
      <Enabled>true</Enabled>
      <ScheduleByDay>
        <DaysInterval>1</DaysInterval>
      </ScheduleByDay>
    </CalendarTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <UserId>test\404</UserId>
      <LogonType>Password</LogonType>
      <RunLevel>LeastPrivilege</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>false</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <IdleSettings>
      <StopOnIdleEnd>true</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
    <UseUnifiedSchedulingEngine>false</UseUnifiedSchedulingEngine>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>PT1H</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>copier.bat</Command>
      <WorkingDirectory>D:\test\</WorkingDirectory>
    </Exec>
  </Actions>
</Task>

When i try to create similar schedule task using below command.

schtasks /create /tn "alarm3" /xml "Test Task.xml"

I am getting error ERROR: Logon failure: unknown user name or bad password.

I want to know how can i create schedule task by using batch file. Schedule task need to be compatible for Win 2003 to Win 2012 server.

Thanks for your time.


Solution 1:

You can use schtasks and provide the task user credentials. I use this regularly in my PowerShell scripts

schtasks.exe /create /s NAMEOFCOMPUTER /ru DOMAIN\USER /rp PASSWORD /tn NAMEOFTASK' /XML PATHTOXMLFILE