Setting New-ScheduledTask Trigger to UTC
I'm trying to figure out how to set a scheduled task trigger to be based on UTC rather than the local timezone of the system but I can't seem to find any documentation on how to do this in Powershell. Via the GUI its just a checkbox. Any hints?
$script = "E:\Data\Scripts\" + $env:COMPUTERNAME.substring(0,3) + $env:COMPUTERNAME.substring(4,2) + ".cmd"
$trigger = New-ScheduledTaskTrigger -At 07:30 -Daily
$action= New-ScheduledTaskAction $script
Register-ScheduledTask -TaskName "Run Scripts" -Trigger $trigger -User user -Password password Action $action
I want the trigger to be 22:00 UTC.
February 24th, 2015 9:07am
try:
((Get-Date -Hour 7 -Minute 30).ToUniversalTime()).ToString("h:m")
February 24th, 2015 9:22am
Just to be sure, I'd do
$triggertime = ((get-date -hour 22 -minute 00).touniversaltime()).ToString("h:m")
$trigger = New-ScheduledTaskTrigger -At $triggertime -Daily
and this would set the task to run at 22:00 UTC? I have to run this against 85 servers across multiple timezones but I need them all to run the task at the same time. I would suspect your example would convert 07:30 from the timezone of the server
the command is run on (let's say GMT-5) to whatever it is in UTC (12:30) but if I run this on a server in GMT-6 it would be 11:30 UTC.
Am I understanding this correctly?
-
Edited by
Eric_0000
20 hours 55 minutes ago
February 24th, 2015 9:33am
Yep, sounds like you got it.
"Get-Date -hour 22 -minute 00" give us a time of 22:00 local time.
.ToUniveralTime() converts it to UTC based on local UTC offset
.ToSting("h:m") put in the hour minute format with no month, year, etc..
February 24th, 2015 9:42am
The big issue is that Task Scheduler will not set it to UTC but will convert that time to local time.
All times in Windows are stored as UTC.
Are you really trying to find the time equivalent at a remote location?
February 24th, 2015 10:25am
Ya i need everyone to run a task at the same time and I'd prefer not to have to calculate it all by hand. Now that I know I wasnt missing something with the task scheduler cmdlets, I'll start writing some lines to calculate the local equivalent.
February 24th, 2015 10:27am
Thanks for this, unfortunately I need to go in the opposite direction. (ie I need to set the task to run at 22:00 UTC so I need to figure out what time that is locally)
February 24th, 2015 10:28am
You can create the DateTime as UTC then convert to local time.
February 24th, 2015 10:39am
You can do it with just numeric values too:
(new-object System.DateTime(2015,11,21,1,10,0,[DateTimeKind]::Utc)).ToLocalTime()
February 24th, 2015 10:57am
invoke-commanded this script across an array of servers and all is well - thanks for your help!
$triggertime = new-object System.DateTime((get-date "22:00").Ticks, "Utc")
$trigger = New-ScheduledTaskTrigger -At $triggertime.ToLocalTime().ToString("HH:mm") -Daily
Set-ScheduledTask TaskName "Run Scripts" Trigger $trigger -User user -Password password
February 24th, 2015 11:46am
Just to be sure, I'd do
$triggertime = ((get-date -hour 22 -minute 00).touniversaltime()).ToString("h:m")
$trigger = New-ScheduledTaskTrigger -At $triggertime -Daily
and this would set the task to run at 22:00 UTC? I have to run this against 85 servers across multiple timezones but I need them all to run the task at the same time. I would suspect your example would convert 07:30 from the timezone of the server
the command is run on (let's say GMT-5) to whatever it is in UTC (12:30) but if I run this on a server in GMT-6 it would be 11:30 UTC.
Am I understanding this correctly?
-
Edited by
Eric_0000
Tuesday, February 24, 2015 2:37 PM
February 24th, 2015 5:33pm