Automation Account How to Stop and start Multiples VMS

Akhila Anne 20 Reputation points
2025-03-06T01:06:19.02+00:00

How to Start and Stop Multiple VMs Using Specific Tag Keys and Values in an Automation Account.

Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,323 questions
0 comments No comments
{count} votes

Accepted answer
  1. Naveena Patlolla 820 Reputation points Microsoft External Staff
    2025-03-06T01:15:14.3133333+00:00

    Hi Akhila Anne

    Welcome to Microsoft Q&A Forum, thank you for posting your query here!

    Please follow the below steps:

    1)Create an Automation Account and ensure that your automation account has the required permissions

    2)Create two runbooks: one for stopping VMs and another for starting VMs.

    Runbook 1:

    Use the following PowerShell script to stop VMs based on specified tags.

    # Connect to Azure with system-assigned managed identity
    $AzureContext = (Connect-AzAccount -Identity).context
    # set and store context
    $AzureContext = Set-AzContext -SubscriptionName $AzureContext.Subscription -DefaultProfile $AzureContext
    # Connect to Azure (if not already connected)
    #Connect-AzAccount
    Select-AzSubscription -Subscription "SubscriptionName"
    # Define the tag key and value
    $TagKey = "Environment"
    $TagValue = "dev-Autoshutdown"
    # Get all VMs that have the specified tag value
    $VMs = Get-AzVM | Where-Object { $_.Tags[$TagKey] -eq $TagValue }
    # Stop the VMs
    foreach ($VM in $VMs) {
        Write-Host "Stopping VM: $($VM.Name) in Resource Group: $($VM.ResourceGroupName)"
        #For Stop use the Below Command in the Stop Dev VMs Run book
        Stop-AzVM -Name $VM.Name -ResourceGroupName $VM.ResourceGroupName -Force
    }
    Write-Host "All dev-test tagged VMs have been powered off."
    

    Runbook 2:

    Use the following PowerShell script to start VMs based on specified tags.

    # Connect to Azure with system-assigned managed identity
    $AzureContext = (Connect-AzAccount -Identity).context
    # set and store context
    $AzureContext = Set-AzContext -SubscriptionName $AzureContext.Subscription -DefaultProfile $AzureContext
    # Connect to Azure (if not already connected)
    #Connect-AzAccount
    Select-AzSubscription -Subscription "SubscriptionName"
    # Define the tag key and value
    $TagKey = "Environment"
    $TagValue = "dev-Autostart"
    # Get all VMs that have the specified tag value
    $VMs = Get-AzVM | Where-Object { $_.Tags[$TagKey] -eq $TagValue }
    # Start the VMs
    foreach ($VM in $VMs) {
        Write-Host "Starting VM: $($VM.Name) in Resource Group: $($VM.ResourceGroupName)"
         #For start use the Below Command in the Start Dev VMs Run book
        Start-AzVM -Name $VM.Name -ResourceGroupName $VM.ResourceGroupName
    }
    Write-Host "All dev-test tagged VMs have been powered on."
    

    Please do not forget to "Accept the answer” and “upvote it” wherever the information provided helps you, this can be beneficial to other community members.it would be greatly appreciated and helpful to others.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.