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.

local time Azure Automation

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.

local time Azure Automation
Featured image: Pixabay

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