使用批处理文件创建任务 - 如果错过则尽快运行任务

使用批处理文件创建任务 - 如果错过则尽快运行任务

使用批处理文件或命令行创建任务时,如何指定以下选项?

Run task as soon as possible after a scheduled start is missed

我知道此选项通过 GUI 存在,但我需要通过命令行指定它。

答案1

没有针对此问题的命令行选项。一种解决方法是使用 GUI 创建带有该选项的任务,然后将其导出到 XML 文件。然后您可以使用

schtasks /create /TN "New Task" /xml "C:\TEMP\New Task.xml" /RU DOMAIN\username /RP password

答案2

Powershell 进行救援,使用 -StartWhenAvailable

Set-ScheduledTask `
    -TaskName $TaskName `
    -TaskPath $TaskPath `
    -Trigger $(New-ScheduledTaskTrigger -At "01.01.2018 05:00:00" -DaysOfWeek $day -Weekly -Verbose) `
    -Settings $(New-ScheduledTaskSettingsSet -StartWhenAvailable -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries)

答案3

也可以使用 VBscript 来完成。在 win 7 中,Powershell 中没有 Set-ScheduledTask。

Set objTaskService = CreateObject("Schedule.Service")
objTaskService.Connect()

Set objRootFolder = objTaskService.GetFolder("\")
Set objTask = objTaskService.NewTask(0)

objTask.RegistrationInfo.Description = "My Scheduled Task"
objTask.Settings.Enabled = True
objTask.Settings.Hidden = False
objTask.Settings.StartWhenAvailable = True 'Run the task as soon as possible after a scheduled start is missed

Set objTrigger = objTask.Triggers.Create(1)
objTrigger.StartBoundary = "2023-04-08T12:00:00"
objTrigger.Enabled = True
objTrigger.Id = "DailyTrigger"
objTrigger.Repetition.Interval = "PT1M"
objTrigger.Repetition.Duration = "PT1H"

Set objAction = objTask.Actions.Create(0)
objAction.Path = "D:\Windows\System32\Notepad.exe"

objRootFolder.RegisterTaskDefinition "MyTask", objTask, 6, , , 3

相关内容