Azure Tip of the Day: Using local time in your Azure Automation

Most of the time, we don’t care that much about the time when running Azure Automation. However, in some specific cases, we need the tasks to check local time to perform some Azure Automation actions. One of my customers wants to start and shutdown VMs based on a schedule, and I don’t want to do the match with Universal Time every time. Also, UTC does not help with the customer time zone changes and changes in the code would be required.

One approach that worked for me was converting the current Get-Date cmdlet to UTC. You may be wondering, Azure is already UTC, but if you want to go back and forth with the same code between on-prem and Azure, having that conversion at the beginning won’t hurt you.

After getting the time in a variable, the second step is to define the time zone that you want to use (in our case EST), and last but not least, convert the time using the initial variable and the time zone defined.

$tempDate =(Get-Date).ToUniversalTime()
$tzEST = [System.TimeZoneInfo]::FindSystemTimeZoneById("Eastern Standard Time")
$CurrentTime = [System.TimeZoneInfo]::ConvertTimeFromUtc($tempDate, $tzEST)

If you want to use that information throughout your script, make sure to use the -Date and use the variable that contains the time in your time zone.

Get-Date -Date $CurrentTime -UFormat %u
(Get-Date -Date $CurrentTime).Hour

We can create a simple PowerShell Runbook and add the code that we have seen so far to get an idea of how it would work.

As we can see, the UTC was 8:08p.m.. and EST was 3:08 p.m. after converting the date information. It is useful for scripts that are sensitive to the local time of the customer instead of Azure Automation.


Featured image: Pixabay

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

Remove virtual machines and virtual hard disks completely with PowerShell

Deleting virtual machines is easy, but if you don’t also remove virtual hard disks, you…

14 hours ago

Secure your WordPress website: Simple steps to stay safe

Many small businesses use WordPress to build their website. And while WordPress has many options…

19 hours ago

Qumulo raises $125M for cloud data management across a hybrid setup

Qumulo is an up-and-coming data management solution focusing on managing files in a hybrid setup.…

4 days ago

Why SMBs need a standalone solution for Windows 10 patch management

Is patch management for the Windows PCs at your business driving you crazy? Maybe there's…

4 days ago

Microsoft Teams guest access: How to enable and manage it

As an IT admin, you can allow guest access to some of your Microsoft 365…

5 days ago

Samsung Galaxy Unpacked 2020: Everything you need to know

Samsung rolled out the all-new Galaxy Z Fold 2, Note 20, Note 20 Ultra handsets…

5 days ago