Run batch file from Windows Task Scheduler regardless of user logged in

北城以北 提交于 2019-12-11 05:07:33

问题


I have a batch file which will copy certain folders from an absolute path over as a backup. I currently need to make it so every midnight the batch file will be executed - and that's during the time when no one is on the computer.

Currently I've set up the task in Windows Task Scheduler to Run whether the user is logged in or not, saving the administrator's password, set a trigger each day at midnight, and start the bat file in its specified location with the path to the folder (without quotation marks) as the working directory.

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.4"     xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2016-10-12T12:47:13.4632673</Date>
    <Author>GROUP\ini</Author>
    <Description>Creates a backup of DemoTestTool daily</Description>
  </RegistrationInfo>
  <Triggers>
    <CalendarTrigger>
      <StartBoundary>2016-10-18T00:00:00Z</StartBoundary>
      <Enabled>true</Enabled>
      <ScheduleByDay>
        <DaysInterval>1</DaysInterval>
      </ScheduleByDay>
    </CalendarTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <UserId>DEMO-TEST-SQL\Administrator</UserId>
      <LogonType>Password</LogonType>
      <RunLevel>HighestAvailable</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>false</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <IdleSettings>
      <StopOnIdleEnd>false</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
    <UseUnifiedSchedulingEngine>false</UseUnifiedSchedulingEngine>
    <WakeToRun>true</WakeToRun>
    <ExecutionTimeLimit>PT4H</ExecutionTimeLimit>
    <Priority>7</Priority>
    <RestartOnFailure>
      <Interval>PT2H</Interval>
      <Count>3</Count>
    </RestartOnFailure>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>\\tsclient\T\Main\Demo- Test_Tool\Programdata\Backup\BackupSys.bat</Command>
      <WorkingDirectory>\\tsclient\T\Main\Demo-Test_Tool\Programdata\Backup</WorkingDirectory>
    </Exec>
  </Actions>
</Task>

Here's the complete setup exported as XML - problem is that I can't simply assume that someone will be online to have the scheduled task run, as it's meant to run automatically when no one is using the system.

My problem is that while the task may be able to run when I manually run it (press the green run icon in the Task Scheduler), the task won't execute at the planned time, possibly due to the \tsclient\T drive not being available at the given time.

Is there any way I can access this drive when there aren't a dedicated user online on the computer?

Network location of backup batch file

The task is configured for Windows Server 2012 R2.

来源:https://stackoverflow.com/questions/40107160/run-batch-file-from-windows-task-scheduler-regardless-of-user-logged-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!