How to use a time zone different than UTC in your Azure runbooks

Most of the time, you don’t need to worry about the time inside of an Azure runbook because the scheduler takes care of the execution time for us. However, sometimes you need to evaluate time within the script, and it is not that easy as you may think. For starters, during the execution, the runbook is running in UTC zone. Although we can use tzutil or Get/Set-TimeZone on our computers, we don’t have the same flexibility in a runbook.

There are tons of ways to achieve the goal of this blog post, and we are going to use these three lines to retrieve the current date in the runbook. The first one is $tDate variable, which will receive the current date information. The second is the $tz variable, where we are going to configure the desired time zone information (in our case EST.) Finally, we will convert our current date information (UTC) to the one that we set (EST).

Note: You can get a complete list of the time zones by running Get-TimeZone -ListAvailable cmdlet.

$tDate =(Get-Date).ToUniversalTime()
$tz = [System.TimeZoneInfo]::FindSystemTimeZoneById("Eastern Standard Time")
$tCurrentTime = [System.TimeZoneInfo]::ConvertTimeFromUtc($tDate, $tz)

Now, when you are running Get-Date to retrieve more information about a specific date, we need to be extra cautious and add -Date $tCurrentTime to use the date that we converted otherwise it will use UTC.

$vCurrentDayofWeek = Get-Date -Date $tCurrentTime -UFormat %u
$vCurrentHour = (Get-Date -Date $tCurrentTime).Hour

In the example above, we will have the hour and day of the week from our variable $tCurrentTime instead of the actual Get-Date output. One thing to keep in mind: We are using the date information from the variable instead of the system, and in some cases that may not be accurate enough.

Featured image: Shutterstock

About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top