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

Anderson Patricio

Anderson Patricio is a Canadian MVP in Cloud and Datacenter Management, and Office Server and Services, besides of the Microsoft Award he also holds a Solutions Master (MCSM) in Exchange, CISSP and several other certifications. Anderson contributes to the Microsoft Community with articles, tutorials, blog posts, twitter, forums and book reviews. He is a regular contributor here at Techgenix.com, MSExchange.org, ITPROCentral.com and Anderson Patricio.org (Portuguese).

Share
Published by
Anderson Patricio

Recent Posts

Conquer the world with PowerShell global variable

In Power Shell, every variable has a certain scope, but a PowerShell global variable is…

46 mins ago

What is DevSecOps and why is it important for your company?

The DevOps philosophy has greatly transformed how technology organizations are run and software development projects…

1 day ago

Machine learning security tools: The good guys strike back

With cyberattacks growing in strength and number, it’s good to know there are machine learning…

4 days ago

Amazon Macie: New enhanced version with lower pricing available

AWS has rolled out an enhanced version of its Amazon Macie security service. The new…

4 days ago

Best patch management tools to keep remote devices updated

Administering remote devices can be a headache for IT pros. But with these patch management…

5 days ago

Managing a Red Hat Enterprise Linux network: Quick-start guide

A good understanding of the Red Hat Enterprise Linux network component is key to any…

5 days ago