Send Email Alerts when Azure VM is Shut down using PowerShell

In this page , I'll explains "Send email alerts if the Azure VMs is deallocated or stopped using PowerShell". This scenerio will help when Azure Virtual machines suddenly stopped due to netwok issue or data center is down. These type of issues we can't monitor every time,so i have explained below article to identifies when VM is stopped.

Prerequisite:

  • Powershell IDE
  • AzureRm Module in powershell
  • Gmail Account

First we need to Azure Rm module:

  • Open PowerShell from your local machine and install the Azure RM Module 
  • And Type the command "install-module azure rm" in your powershell command window
  • By default it will give  yes by typing [Y] to install NuGet Provider. 
  • Wait for 5 min. Azure Rm module installing.

PowerShell commands to send email alerts: 

After sucessfully installing the AzureRm module, copy below powershell script into your local.

Below code reads the all VM under in a specific resource group.  

PowerShell Script:

Login-AzureRmAccount    # First time this command prompt for Azure authentication

function CheckPowerStatus($ResourceName){   

write-host "Your are looking VM in this RG: " $ResourceName 

$ResourceGroupColl = Get-AzureRMResourceGroup 

$ResourceGroup= $ResourceName  

Write-Host $ResourceGroup.ResourceGroupName

 $VirtualMachines = Get-AzureRmVM -ResourceGroupName $ResourceGroup  

#Read All VM in a Azure Resource Group

 foreach($VirtualMachine in $VirtualMachines)  {  

$VMInfo = Get-AzureRmVM -ResourceGroupNamae $ResourceGroup -Name $VirtualMachine.Name -Status 

        Write-Host $VMInfo.Name  # Displaying VM name

        foreach ($Status in $VMInfo.Statuses)  {   

            if($Status.Code -like "PowerState/*")  {   

                $VMDetails = $Status.DisplayStatus  

                Write-Host $VMDetails   # Display VM PowerStatus and add below

                if($VMDetails ="VM running") {   #Checking VM PowerStatus , currently i am sending for running status

                     $VMName = $VirtualMachine.Name     

                    #Sending email alerts based on PowerStatus

                     $BodyDetails = $Body + " <br /> VM Name : $VMName    -----  $VMDetails "      

                     $Message = new-object Net.Mail.MailMessage 

                     $smtp = new-object Net.Mail.SmtpClient("smtp.gmail.com", 587)  

                    $smtp.Credentials = New-Object System.Net.NetworkCredential ("XXXX@gmail.com",                                                         "Your Gmail Password");       

                      $smtp.EnableSsl = $true 

                     $smtp.Timeout = 400000  

                     $Message.From = "xxxxxxxx@gmail.com" 

                     $Message.To.Add("toadresss@gmail.com") 

                     $Message.Subject=  $VMName+" VM Stopped"

                     $Message.Body= "Your Azure VM:  "+$VMName +" is stopped. Please check."

                     $smtp.Send($Message)

                      Write-Host "Email Sent"

                }

            }  

        }      

    }       

  }

    

Comments

Popular posts from this blog

Sample Chatbot flow diagram

Architecture diagram for upload & validate the videos in azure