Thursday, 8 June 2017

Automate Start and Shutdown of an AZURE VM


Automate Start and Shutdown of Azure VM



We can manage an automation of AZURE VM Start and Stop Activity easily with the help of Powershell. The purpose of this automation process is to avoid /reduce cost if the service is not required in specific timings (For example, if there is an application Virtual Machine which needs to be in running state only in day time and should be shut down in the night hours). Many times we forget to do the manual shutdown and end up paying a lot of unnecessary Bill. To avoid such scenarios this automation will help us to schedule a job as per our requirement as we know each service in azure is directly proportional to the Cost.
Let’s have a quick review about the automation process along with screenshots.
  1. Let’s create an automation account first on Azure portal.
Automate Start and Shutdown of Azure VM
Please fill the required details in below screen this will get by click on automation account on above screen, then click on create tab it will take few seconds to deployment.
Automate Start and Shutdown of Azure VM
After account is visible to you, click on account name then you will be redirected to below screen.
Automate Start and Shutdown of Azure VM
Then click on Runbook new runbookfill with required details as below then create it.
Automate Start and Shutdown of Azure VM
Runbook is kind of platform to execute set of PowerShell scripts to achieve our goal of automation.

After run-book is created you should get a window like given below where you can write your set of execution of PowerShell scripts. This run-book will be used to restart virtual machine every Sunday. Save and publish the run-book code and execute it by clicking test pane tab as shown below.
Automate Start and Shutdown of Azure VM
To get an email notification whenever VM starts/Stops or Restart through schedule, we need to create it by below given process.
Click on automation accountassetscredentials tab and fill the required details as shown below and we can schedule it from same window as well .
Automate Start and Shutdown of Azure VM
Schedule it as below.
Automate Start and Shutdown of Azure VM



Below given PowerShell script will be useful to create a runbook to restart VM-
<# .DESCRIPTION Script for starting VM when Base VM Memory reaches above 85 % for last 10 minutes .NOTES AUTHOR: Azure Automation Team LASTEDIT: May 16, 2017 #> $connectionName = "AzureRunAsConnection" try { # Get the connection "AzureRunAsConnection " $servicePrincipalConnection=Get-AutomationConnection -Name $connectionName "Logging in to Azure..." Add-AzureRmAccount ` -ServicePrincipal ` -TenantId $servicePrincipalConnection.TenantId ` -ApplicationId $servicePrincipalConnection.ApplicationId ` -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint "Successfully logged in to Azure Account" } catch { if (!$servicePrincipalConnection) { $ErrorMessage = "Connection $connectionName not found." throw $ErrorMessage } else{ Write-Error -Message $_.Exception throw $_.Exception } } # Create Email Data $smtpServer = "smtp.office365.com" $smtpFrom = "Alerts@silicus.com" $smtpTo = "Mastanvali.Shaik@silicus.com" $messageSubject = "[VM] by weekly scheduled reboot" $message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto $message.Subject = $messageSubject $message.IsBodyHTML = $True "Checking server status" $vmStatus = Get-AzureRmVM -ResourceGroupName shrinicloud -Name DontTouch -Status if($vmStatus.Statuses[1].Code -eq "PowerState/running") { "Server Status - Running" "Restarting VM" $opstatus = Restart-AzureRmVM -Name DontTouch -ResourceGroupName shrinicloud if ($opstatus.Status -eq "Succeeded") { "Server successfully Restarted" $EmailMessage = "VM xxx successfully restarted" } else { "Error starting server, please contact support." $EmailMessage = "Error starting server [VMName], please contact support." } $message.Body = $EmailMessage $smtp = New-Object Net.Mail.SmtpClient($smtpServer) $cred = Get-AutomationPSCredential -Name "EmailAccount" $smtp.Credentials = $cred $smtp.EnableSsl = $True $smtp.Send($message) "Email alert send" } else { "Server status already Running" }

So, We have seen that we can Automate Start and Shutdown of Azure VM by following these simple steps.

0 comments:

Post a Comment