Azure Automation – How to Automatically Start and Stop Azure VMs using Azure Automation

In this tutorial, you will learn how you can use Azure Automation Account to automate starting and stopping of Azure Virtual Machines. This feature will allow you to create custom schedules that are recurring.

Managing the uptime of your Azure Virtual Machines (VMs) is essential for both cost-efficiency and operational effectiveness. One way to manage this is by automating the start and stop schedules for your VMs. Azure Automation provides a great feature for this task, allowing you to create custom schedules that help optimize costs of your Azure Virtual Machines.

In this blog post, we will walk through the step-by-step process of setting up a custom start and stop schedule for your Azure Virtual Machines using Azure Automation.

Prerequisites
Before we begin, make sure you have the following:

  • You need an active Azure subscription
  • You need necessary permissions (Owner/Contributor) to create resources in your Azure subscription
  • You need Azure Virtual Machines – Have the VMs already deployed in your Azure environment.
  • You need to have Az PowerShell module installed in your Azure VMs

Run below PowerShell commands (one at a time), As Administrator to confirm if Az PowerShell module is installed on an Azure VM.

  • $PSVersionTable #Note – PS Version has to be either 5.1.X (minimum) or 7.2.X (recommended)
  • Get-ExecutionPolicy
  • Set-ExecutionPolicy RemoteSigned
  • Install-Module -Name Az -Repository PSGallery -Force

Further instructions on How to install Az PowerShell Module can be found here.

Step 1: Create an Automation Account

  • Log in to portal.azure.com
  • Search for Automation Accounts in the Search Bar
  • Create an Automation Account (System Assigned)

Step 2: Configure the Automation Account for VM Management

  • Once the Automation Account is deployed, navigate to it in the Azure portal.
  • We need to create two Runbooks (one for Starting an Azure VM and another one for Stopping an Azure VM)
    • In the Automation account menu, select Runbooks.
    • Under Runbooks, select Create a Runbook.
    • Name the Runbook (e.g., “Start-VMs” for Starting an Azure VM).
    • Runbook type = PowerShell and Runtime version = either 5.1 or 7.2 (Depending on the PowerShell version installed on your VMs
    • Create Runbook.
  • Repeat the above steps to create a second Runbook for Stopping an Azure VM
  • Once the Runbooks are created, you’ll see a code editor where you can input PowerShell commands to start and stop your VMs.
  • Add below scripts respectively and remember to update the script with your values for Subscription ID, Resource Group and VMname and Publish the Runbooks

To Start an Azure VM

param(
[string]$ResourceGroupName = “ResourceGroup”,
[string]$VMName = “VMname”
)

#”Logging in to Azure…”

Connect-AzAccount -Identity

#Set the context to a specific subscription

Set-AzContext -SubscriptionId “subscriptionid”

#Start the VM

Start-AzVM -ResourceGroupName $ResourceGroupName -Name $VMName

To Stop an Azure VM

param(
[string]$ResourceGroupName = “ResourceGroup”,
[string]$VMName = “VMname”
)

#”Logging in to Azure…”

Connect-AzAccount -Identity

#Set the context to a specific subscription

Set-AzContext -SubscriptionId “subscriptionid”

#Stop the VM

Stop-AzVM -ResourceGroupName $ResourceGroupName -Name $VMName

  • Click on each Runbook and Add (Link) your desired Schedule

Step 3. Set Permissions on Azure VM(s)

  • Provide IAM (RBAC Role = Virtual Machine Contributor) to the Managed Identity (Automation Account – System Assigned)


Step 4: Test the Automation

To test your Runbooks manually (to confirm everything works as expected)

  • In the Automation account, go to Runbooks, select your Runbook (e.g., “Start or Stop VMs”), and click Start to test the automation.
  • Verify that your VMs are correctly starting and stopping according to the script.
  • Check the Job Status

Once the Runbook is triggered, you can check the Jobs section to view the status of the execution.
Ensure that there are no errors and that the VMs start and stop as expected.

That is it! Congratulations for completing this task! If you have any questions, put a comment in my YouTube tutorial! 🙂