Azure – automate VM start up and shutdown on a schedule

Hi there

in this article I’m covering how to save costs on your VM environment by scheduling a time to start and stop your VM on a schedule.

Create an Azure Automation account

  • go to Azure Portal home \ marketplace \ search for automation
  • create an azure automation account
  • give it a name:
  • finish the wizard
  • wait until the azure automation account is created and go to the resource

Create and publish a runbook

  • go to Azure Portal home \ marketplace \ search for automation
  • go to your azure resource group
  • select the azure automation account you created
  • click on runbooks
  • click on create a runbook “+”
  • give it a name, at resource type select Powershell and give it a descripton and click create
  • at the next screen “cmdlets”
  • paste the following code (powershell commands that’ll parse the VM name, Azure Resource group – where the vm is located and the parameters to start or stop the vm) than click save and publish
Param(
[string]$VmName,
[string]$ResourceGroupName,
[ValidateSet("Start", "Shutdown")]
[string]$VmAction
)
# Authenticate Azure Automation Account
$Conn = Get-AutomationConnection -Name AzureRunAsConnection
Add-AzureRMAccount -ServicePrincipal -Tenant $Conn.TenantID `
-ApplicationID $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint
# Start VM
IF ($VmAction -eq "Start") {
Start-AzureRmVM -Name $VmName -ResourceGroupName $ResourceGroupName
}
# Stop VM
IF ($VmAction -eq "Shutdown") {
Stop-AzureRmVM -Name $VmName -ResourceGroupName $ResourceGroupName -Force
}

 

Create your recurring schedule

  • under the runbook
  • select Schedules blade under Resources menu 
  • you need to create two different schecules one to start and another to stop the vm daily:
    • daily01 : starts the vm daily at 9 AM EST on my scenario
    • daily02: stops the vm daily at 6 PM EST on my scenario

 

Now wait until the time and date scheduled to next run to see it in action.

Check my Github repository

References
https://azure.microsoft.com/en-us/services/automation/
https://docs.microsoft.com/en-us/azure/automation/
https://docs.microsoft.com/en-us/azure/automation/automation-runbook-types

Thanks,

Thiago Beier
TwitterLinkedInFacebookRSS