![透過批次檔從xml檔案建立排程任務](https://rvso.com/image/1481847/%E9%80%8F%E9%81%8E%E6%89%B9%E6%AC%A1%E6%AA%94%E5%BE%9Exml%E6%AA%94%E6%A1%88%E5%BB%BA%E7%AB%8B%E6%8E%92%E7%A8%8B%E4%BB%BB%E5%8B%99.png)
我的測試機器中有一個計劃任務。測試機器是 win 7。
我計劃為計劃任務建立一個批次檔。由於大多數機器都在同一域中。因此,我計劃使用服務帳戶進行身份驗證。
下面是 xml 檔。目前,任務調度程序運作良好。
<?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>
當我嘗試使用以下命令建立類似的計劃任務時。
schtasks /create /tn "alarm3" /xml "Test Task.xml"
我收到錯誤 錯誤:登入失敗:使用者名稱未知或密碼錯誤。
我想知道如何使用批次文件建立計劃任務。計劃任務需要相容贏得2003年到贏2012伺服器。
謝謝你的時間。
答案1
您可以使用schtasks
並提供任務使用者憑證。我經常在我的 PowerShell 腳本中使用它
schtasks.exe /create /s NAMEOFCOMPUTER /ru DOMAIN\USER /rp PASSWORD /tn NAMEOFTASK' /XML PATHTOXMLFILE