Create scheduled task using Task Scheduler Managed Wrapper with “Synchronize across time zones” option disabled

徘徊边缘 提交于 2020-12-12 09:22:27

问题


Does anybody know how to create a scheduled task using Task Scheduler Managed Wrapper or Schtasks.exe with "Synchronize across time zones" unchecked.

Windows Task Scheduler - Task Trigger - Synchronize across time zones


回答1:


You can do this with schtasks.exe, but it's tricky. Essentially, you have to use the /xml switch and pass an XML file that has the trigger formatted properly.

The basics of the XML file can be determined by getting as much of the required config done in the Task Scheduler GUI on your dev machine; then using Export... from the context menu, saving the file and chopping out the irrelevant bits.

Given a basic XML structure:

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <Triggers>
    <CalendarTrigger>
      <StartBoundary>2018-03-28T18:00:00Z</StartBoundary>
      <Enabled>true</Enabled>
      <ScheduleByDay>
        <DaysInterval>1</DaysInterval>
      </ScheduleByDay>
    </CalendarTrigger>
  </Triggers>
  <Actions Context="Author">
    <Exec>
      <Command>C:\Windows\System32\cmd.exe</Command>
      <Arguments>/c dir</Arguments>
    </Exec>
  </Actions>
</Task>

The important element here is <StartBoundary/> – it defines both the date/time from which tasks should start running and (in the case of time-based triggers) the time at which it should run each day, week, etc.

If you want Synchronize across timezones to be unchecked: You must use a time value for start boundary that is as per the local time that you want the task to run and does not end with the GMT+0/UTC+0/zulu-time indicator Z:

<StartBoundary>2018-03-28T18:00:00</StartBoundary>

The above should run every day, at 18:00 local time.

If you want Synchronize across timezones to be checked: You must calculate the GMT+0/UTC+0/zulu-time of the desired start time yourself, based on your local timezone and respective of any daylight-savings system your timezone uses, then use this time value and include the Z indicator at the end:

<StartBoundary>2018-03-28T18:00:00Z</StartBoundary>

The above should run every day, at 18:00 UTC, regardless of local time.

To register the above task: From the command prompt, you would issue:

schtasks.exe /create /tn "My Task Name" /xml x:\pathto\taskdefinition.xml

(You do not need to keep the task definition file after you have registered it; the settings are copied to the created task.)

The difficulty here is probably in creating the XML file — it can be a little finicky around the encoding of the file (you may need to experiment with byte-order markers), and there are some combinations of settings that I have never been able to get to run properly (they register OK, but the running task instantly fails with a strange return code). Your mileage may vary.

I've never tried with the Managed Wrapper, but the source suggests it also generates the XML in the background. However, it appears to use XmlDateTimeSerializationMode.RoundtripKind as its serialization method, which (rightly, for round-tripping) includes the timezone as part of the serialization.

This leads me to think that it will never create a task that has Synchronize across timezones unchecked. In fact, it may mean that, if you can determine the correct timezone suffix for your start time, you might not need to do that Z-based calculation above, yourself.

You might be able to raise a feature request, to have this changed based on a boolean property, e.g.:

writer.WriteElementString("StartBoundary", 
                          System.Xml.XmlConvert.ToString(t.StartBoundary, 
                          System.Xml.XmlDateTimeSerializationMode.RoundtripKind));

becoming:

writer.WriteElementString("StartBoundary", 
                          System.Xml.XmlConvert.ToString(t.StartBoundary, 
                          SynchronizeAcrossTimezones 
                              ? System.Xml.XmlDateTimeSerializationMode.RoundtripKind
                              : System.Xml.XmlDateTimeSerializationMode.Unspecified));

...but that's not up to me!




回答2:


Fixed it using the Task Scheduler Managed Wrapper library by specifying the DateTimeKind.Unspecified in StatBoundary



来源:https://stackoverflow.com/questions/48363828/create-scheduled-task-using-task-scheduler-managed-wrapper-with-synchronize-acr

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