Streamline Your Workflow: Automate Azure VM Start/Stop with Azure Automation

SAURAV PADHYSAURAV PADHY
3 min read

Azure Automation is a Microsoft Azure service that helps you save time and reduce mistakes by automating common IT tasks. With Azure Automation, you can automate workflows, manage system settings, and handle software updates more efficiently.

STEPS

1. Create an Automation Account

  1. Go to the Azure Portal.

  2. Search for Automation Accounts and click + Create.

  3. Provide the required details:

    • Subscription: Select your subscription.

    • Resource Group: Use an existing one or create a new one.

    • Automation Account Name: Example: MyAutomationAccount.

    • Region: Select the closest region to your resources.

    • Leave the other settings as default and click Review + Create.

  4. Once validated, click Create.

2. Import Azure Modules

  1. Inside your Automation Account, go to Modules under the Shared Resources section.

  2. Search for and import the following modules if not already available:

    • Az.Accounts

    • Az.Compute

  3. This step ensures that your runbook can interact with Azure resources.

3. Create Runbook

  1. Navigate to Runbooks under the Process Automation section in your Automation Account.

  2. Click + Create a Runbook.

  3. Provide the following details:

    • Name: Example: StartStopVM.

    • Runbook Type: PowerShell.

    • Runtime Version: Use the latest.

  4. Click Create.

4. Enable Managed Identity

  • Navigate to your Automation Account in the Azure portal.

  • Go to Identity under the Settings section.

  • Turn on the System Assigned Managed Identity and click Save.

5. Assign Role Permissions

  • Go to your VM Resource Group or Subscription in Azure.

  • Select Access Control (IAM) > + Add > Add role assignment.

  • Assign the Contributor role to the Automation Account's managed identity.

6. Add the Runbook Script

param(
    [string]$Action  # 'Start' or 'Stop'
)

# Authenticate using Managed Identity
Connect-AzAccount -Identity

# Set the subscription context (optional if only one subscription is used)
# Replace 'your-subscription-id' with the actual Subscription ID
Set-AzContext -SubscriptionId "your-subscription-id"

# Get all VMs in the subscription
$allVMs = Get-AzVM

foreach ($vm in $allVMs) {
    $vmName = $vm.Name
    $resourceGroupName = $vm.ResourceGroupName

    try {
        if ($Action -eq "Start") {
            # Start the VM
            Start-AzVM -ResourceGroupName $resourceGroupName -Name $vmName -NoWait -ErrorAction Stop
            Write-Output "Starting VM: $vmName in Resource Group: $resourceGroupName"
        } elseif ($Action -eq "Stop") {
            # Stop the VM
            Stop-AzVM -ResourceGroupName $resourceGroupName -Name $vmName -Force -NoWait -ErrorAction Stop
            Write-Output "Stopping VM: $vmName in Resource Group: $resourceGroupName"
        } else {
            Write-Output "Invalid Action: $Action. Use 'Start' or 'Stop'."
        }
    } catch {
        Write-Output "Failed to process VM: $vmName in Resource Group: $resourceGroupName. Error: $_"
    }
}

7. Test Runbook Script

  • Run the runbook manually and provide the action (Start or Stop) as a parameter

8. Schedule the RunBook

  • To start all VMs at a specific time (e.g., 9 AM) and stop them later (e.g., 6 PM), create two schedules:

    • One for Start with the action Start.

    • Another for Stop with the action Stop.

0
Subscribe to my newsletter

Read articles from SAURAV PADHY directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

SAURAV PADHY
SAURAV PADHY

Cloud Engineer(AWS | | AZURE ) | | DevOps | | Python | | Networking | | Data Science | | Jenkins | | Terraform | | Kubernetes | | Azure Virtual Desktop Experienced Python Developer with a demonstrated history of working in the software industry. Skilled in Devops Tools [Docker,Kubernetes,Ansible,Terraform] and Having good Scenario Based Implementation Of Azure and Aws Cloud Infrastructure & Automation