Powershell register scheduled task that respects daylight savings time

Warlike Chimpanzee

I use PowerShell scheduled tasks via the Register-ScheduledTask and New-ScheduledTaskTrigger cmdlets to create reminders for myself throughout the day. When Daylight Savings Time hit, all those reminders became an hour off.

What can I do to avoid this? It would probably require the task triggers to be in local time.

Warlike Chimpanzee

The scheduled task trigger must be modified to be in local time.

  1. Create your trigger as normal

    $trigger = New-ScheduledTaskTrigger -Daily -At 10:15am
    
  2. Then edit the trigger's StartBoundary property:

    $trigger.StartBoundary = [DateTime]::Parse($trigger.StartBoundary).ToLocalTime().ToString("s")
    

Explanation: When you create the trigger with New-ScheduledTaskTrigger, the time you specify is converted to UTC time and saved as a string in the trigger's StartBoundary property. On my machine, 10:15am produces a $trigger.StartBoundary of "2017-12-19T15:15:00Z", where the "Z" indicates UTC time. To specify a local time, we need to convert this date back into local time and remove the "Z"; we want "2017-12-19T10:15:00". The snippet above parses the date string, converts it to local time, and formats it in the correct format.

If you need to do this a lot you may find this function helpful:

function Fix-Trigger {
    param( [parameter(ValueFromPipeline)] [CimInstance] $trigger )
    $newTrigger = $trigger.Clone()
    $newTrigger.StartBoundary = [DateTime]::Parse(
        $trigger.StartBoundary).ToLocalTime().ToString("s")
    $newTrigger
}

# Example usage:
$trigger = New-ScheduledTaskTrigger -Daily -At 10:15am | Fix-Trigger

This is all from here.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

logfiles and daylight savings time

Python daylight savings time

Daylight savings time in java

python time module: daylight savings

Moment handling daylight savings time

Run Node task/function at specific time daily while accounting for Daylight Savings

Python pytz, zoneinfo and daylight savings time

python: UTC to local daylight savings time

How to find the date and time daylight savings begins

Daylight savings time in Angular/Javascript Date

Handling date/time in java/android and daylight savings

Determining whether a timezone supports Daylight Savings Time

Laravel - Carbon Not Factoring in Daylight Savings Time

UIDatePicker does not respect Daylight Savings Time

Joda-time daylight savings in Jordan

Java8 and Daylight Savings Time

SAS daylight savings time conversion SAS

Handling time zone offset transition and daylight savings time with Joda

java.time discovering I'm in the daylight savings "gap"

golang timezone daylight savings time doesn't work

Spark: converting GMT time stamps to Eastern taking daylight savings into account

Dart/Flutter Not recognizing yesterday because of daylight savings time

How to consider daylight savings time when using cron schedule in Airflow

How to get daylight savings start and end for a time zone in Dynamics CRM?

How To Get The Correct Time Zone Offset (In And Out Of Daylight Savings)

How to make Slack work with new Brazilian daylight savings time information?

Check if Date is in Daylight Savings Time for Timezone Without pytz

Using python to determine if a timestamp is under daylight savings time

Weird as.POSIXct behavior depending on daylight savings time