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

Hardening your technology infrastructure in preparation for a DDoS attack

By establishing these 11 appropriate controls beforehand, your organization will be better positioned to withstand and survive a DDoS attack.

1 hour ago

Microsoft App-V as an application virtualization solution: Pros & cons

If your shop is considering using App-V as an application virtualization solution, read this article first and weigh the pros…

4 hours ago

Ransomware threats: Cybercriminals take their wares to the next level

As companies and individuals harden their defenses against ransomware, hackers are creating new and more virulent ransomware threats.

21 hours ago

AWS vs. Google Cloud: After a big 2019, what does 2020 hold?

AWS is the public cloud market leader. But Google Cloud is catching up. One thing is sure: In the AWS-Google…

1 day ago

7 web design principles that are crucial to know for 2020

What is going to be significant in the world of web design over the course of 2020? Let’s take a…

1 day ago

Mobile device management with Microsoft Intune: An introduction

Microsoft Intune is a great and powerful tool. Here’s how to use it for mobile device management to keep track…

2 days ago