Turn Off Azure VM using Azure Automation

In this blog post, I will show you how I turn on an Azure Virtual Machine with Azure Automation.

Azure Automation

Microsoft Azure Automation allows us to automate and schedule a task in Azure using PowerShell scripts.


To manage virtual machines in Microsoft Azure, I will use the following module.



In the runbook, I will use the following script.

Please change the following details to match your environment:




$connection = Get-AutomationConnection -Name AzureRunAsConnection

# Wrap authentication in retry logic for transient network failures
$logonAttempt = 0
while(!($connectionResult) -And ($logonAttempt -le 10))
    # Logging in to Azure...
    $connectionResult =    Connect-AzAccount `
                               -ServicePrincipal `
                               -Tenant $connection.TenantID `
                               -ApplicationId $connection.ApplicationID `
                               -CertificateThumbprint $connection.CertificateThumbprint

    Start-Sleep -Seconds 1
Set-AzContext -SubscriptionId "SUBSCRIPTIONID"
stop-AzVM -ResourceGroupName RGGROUP  -Name VMNAME -force

To start an Azure VM using Azure Automation use this post.

Leave a comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.