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

Who says configuration management can’t be fun?

Managing change in an enterprise isn’t easy and it’s usually no fun. Here’s a book on configuration management that will…

2 hours ago

Choosing the right communication tools for your business

Choosing communication tools is like choosing a first progamming language. While you want easy, you also want cross-platform, security, and…

5 hours ago

Ignite 2019: Azure Arc extends Azure management across infrastructures

In one of the biggest announcements at this month's Ignite 2019, Microsoft gave us details Azure Arc, a new set…

21 hours ago

Your eyes are not playing tricks: New Azure Portal features

If you logged into Azure Portal over the past few days, you may have suffered a little disorientation. Some new…

24 hours ago

Sky is falling: Will the cloud end up bankrupting your small business?

Cloud computing offers many benefits to small businesses, but it also brings certain risks, including the risk of bankrupting your…

1 day ago

Managing accelerated networking in Azure IaaS virtual machines

Configuring your IaaS Azure virtual machines to take advantage of accelerated networking can vastly improve network performance. Here’s how to…

1 day ago