How to auto start and stop Azure VM’s (Manual)

There are situations where you would like to automatically shutdown and start virtual machines in Azure. Where there are several different ways to automate the stop, deallocate and start virtual machines, I would like to show you today how to use a simple setup to automate, schedule and configure stop and start using a simple script.

image

So what are we going to do?

We are going to build the following:

  • Azure Automation Account
  • Azure Automation Runbook with Parameters
  • We will use resource tags on virtual machines to assign a schedule
  • Multiple jobs for different schedules

With this simple approach you will be able to setup a stop and start schedule in less than 10 minutes. So lets get started.

Manual

STEP 1: First we will start with creating an Automation Account from the Azure Portal.

image

Fill in the required fields and click on Next

image

From the Advanced page make sure that the System assigned identity is checked.

image

Continue through the next fields, adjust them to your need or leave them default and Create the new automation acocunt.

image

From the newly created automation account Create a Runbook

image

Fill in the name for your runbook, make sure to select PowerShell with runtime version 7.2 or newer. Click through the rest of the fields and create the runbook.

image

When the runbook has been created you will arrive in the Edit screen. Copy and paste the following code into the next screen. I will explain how it works later, for now from the top you can see parameter value’s that will be retrieved from the schedule that we will create in the last step.


Param(
  [Parameter(Mandatory = $true)]
  [String]
  $TagName,
  [Parameter(Mandatory = $true)]
     
  [String]
  $TagValue,
  [Parameter(Mandatory = $true)]
  [Boolean]
  $Shutdown
)

  # Ensures you do not inherit an AzContext in your runbook
  Disable-AzContextAutosave -Scope Process
  # Connect to Azure with system-assigned managed identity (Azure Automation account, which has been given VM Start permissions)
  $AzureContext = (Connect-AzAccount -Identity).context
  Write-Output -InputObject $AzureContext
  # set and store context
  $AzureContext = Set-AzContext -SubscriptionName $AzureContext.Subscription -DefaultProfile $AzureContext
  Write-Output -InputObject $AzureContext

  $vms = Get-AzResource -TagName $TagName -TagValue $TagValue | Where-Object -FilterScript {
    $_.ResourceType -like 'Microsoft.Compute/virtualMachines' 
  }

  Foreach ($vm in $vms) 
  {
    if ($Shutdown -eq $true) 
    {
      Write-Output -InputObject "Stopping $($vm.Name)"        
      Stop-AzVM -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName -Force
    }
    else 
    {
      Write-Output -InputObject "Starting $($vm.Name)"        
      Start-AzVM -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName
    }
  }

It should look like this, make sure to click Save and Publish

image

STEP 2: In this step we are going to assign permission to the system assigned identity to start and stop virtual machines. Depending on your needs, you can assign permissions on the resource, resource group or subscription.

In this example we are going to assign permissions on a virtual machine level. From your resource go to Access control and click on Add

image

Search for Virtual Machine Contributor and go on to the next screen

image

Make sure to search for your automation account name and select it here. Review and assign the permissions.

image

STEP 3: Configuring virtual machines

In this step we are going to assign tags to our virtual machines to tell which schedule applies to them. So go to your virtual machine and create a tag with the name: powerstate_automation and a value of schedule1. Don’t forget to click apply at the bottom of the page.

image

STEP 4: Create schedules

This is the last step already. From your Azure Automation Account go to schedules and click on Add a schedule

image

In this example we want to shutdown the machine at the end of each business day. So let’s create a schedule for that:

image

Now go back to your runbook, click on Schedules and again on Add a schedule

image

From the new page we are going to connect the created schedule to this runbook and we are going to configure the parameters. First click on Link a schedule to your runbook

image

After that click on Parameters and run settings

image

In the next step fill in the following fields:

Tagname: powerstate_automation

Tagvalue: schedule1

Shutdown: True (If you want the VM to start select false)

image

Click OK to save. Now follow the last steps to create a start schedule.

When completed the result should look like this:

image

Add a Comment

Your email address will not be published. Required fields are marked *