How to Monitor Azure VM running status

How to Monitor Azure VM running status

Microsoft Azure offers 100s of Virtual Machines for different purposes. Some high-end Virtual Machines like M128 have 128 vCPUs, and 2048 GB RAM, which cost $27 per hour. Don’t imagine the bill’s outcome if you left the Virtual Machine ON and forgot to turn it Off. 

So, in this blog, I will share with you the most efficient way to monitor your Azure Virtual Machine running status, and you can get notified how long your Azure high-end VM is running. It will help you in various ways to save the bills.

What happen if you didn't monitor the VM running status

In my workplace, my production team uses an F16s Virtual Machine with (16 vCPUs, and 32 GB RAM), costing $1.7 per hour. Whenever they need the VM, I am responsible for Turning it On and Shutdown after their usage.

Since this is a Virtual Machine connected to a Remote Desktop, we have to minimize the Window after a few minutes and never know what happened inside.

In some scenarios, the VM was running idle for so many hours, and we got billed by Azure (not their mistake). Like sometimes users forget to inform after their usage, or they are using at midnight etc. etc.,

How to get notification if VM is running more than specified hours using PowerShell

For example, if a VM runs for 5 hours, you want to get notified every 1 hour after. Because the VM might be running idle or for any other reason. Azure doesn’t have any native way to get notified once the VM is started and for how long it runs, even with azure monitors or Azure alerts.

PowerShell script to monitor the up time

I use the following PowerShell Script, which sends the email notification to their email address when triggered.

I trigger this script using Task Scheduler. When the VM is started, the task scheduler gets started and running. Once the VM running time reaches 3 hours, it will trigger the below PowerShell script, which sends the preformatted text to certain people. 

Once you get the email, you can check your VM’s status if you still want the VM running and get an additional notification after the 4th or 5th hour. Then you can change the sleep time in the middle of the script according to your needs, change the preformatted text, and repeat the same.

#PowerShell Script to monitor the uptime
#This script will send an email notification to a single or group of people when the VM is running for more than 3 hours

$EmailFrom = “username@domain.com
$EmailTo = “user1@domain.com, user2@domain.com, user3@domain.com
$Subject = “Alert: Azure VM running more than 3 hours
$Body = “Azure High-end VM (Name) running for more than 3 hours. Please Turn OFF the VM if not in use..
$SMTPServer = “outlook.office365.com”
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential(“username@domain.com”, “password”);
$SMTPClient.Send($EmailFrom,$EmailTo,$Subject,$Body)

# set the script to sleep for 1 Hour

start-sleep 3600

$EmailFrom = “username@domain.com
$EmailTo = “user1@domain.com, user2@domain.com, user3@domain.com
$Subject = “Second Alert: Azure VM running more than 4 hours
$Body = “Azure High-end VM (Name) running for more than 4 hours. Please Turn OFF the VM if not in use..
$SMTPServer = “outlook.office365.com”
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential(“username@domain.com”, “password”);
$SMTPClient.Send($EmailFrom,$EmailTo,$Subject,$Body)

Copy the above script, and open a new notepad and PowerShell editor. Paste the script and edit all the required information in bold.

Mohammed Nihal
Latest posts by Mohammed Nihal (see all)

Was this Article Helpful?

Did I just helped you solve one of your problem? Support me by buying me a coffee. Thanks for your support

Leave a Reply