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.
The scheduled task trigger must be modified to be in local time.
Create your trigger as normal
$trigger = New-ScheduledTaskTrigger -Daily -At 10:15am
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.
Comments