How to Monitor Azure VM running status

Microsoft Azure offers 100s of Virtual Machines for different purposes. Some of the high-end Virtual Machines like M128 have 128 vCPUs, 2048 GB RAM which cost $27 per hour. Don’t imagine the outcome of the bill, 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 is 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, 32 GB RAM) and costs $1.7 per hour. Whenever the need the VM I am responsible to Turn On the VM and Shutdown after their usage.

Since this is a Virtual Machine connected with a Remote Desktop we have to minimize the Window after a few minutes and never knew what is happing inside.

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

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

For example, if a VM is running for 5 hours you want to get notified and 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 is running 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 it’s triggered.

I trigger this script using Task Scheduler. When the VM is started the task scheduler gets started and running. Once VM running time reach 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 the status of your VM, if still you want the VM running and get an additional notification after the 4th or 5th hours. Then you can change the sleep time in the middle of the script according to your needs and 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 Turned 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 Hours

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 Turned 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, open a new notepad and PowerShell editor. Paste the script and edit all the required information which is in bold.

Was this Article Helpful?

Leave a Reply

%d bloggers like this: