How to Schedule Azure VM Start (Startup) and Stop (Shutdown)?

# List all the virtual machines
Get-AzVM 

#This command gets properties for all the virtual machines in the resource group named Cloud-Operations.
Get-AzVM -ResourceGroupName "Cloud-Operations"

#This command starts the virtual machine named "Cloud-Ops-VM1" in Cloud-Operations Resource Group.
Start-AzVM -ResourceGroupName "Cloud-Operations" -Name "CloudOps-VM1"

# This command stops the virtual machine named named "Cloud-Ops-VM1" in "Cloud-Operations" Resource Group.
Stop-AzVM -ResourceGroupName "Cloud-Operations" -Name "CloudOps-VM1"

Start-AzureRmVM -ResourceGroupName "Cloud-Operations" -Name "CloudOps-VM1"

Stop-AzureRmVM -ResourceGroupName "Cloud-Operations" -Name "CloudOps-VM1"

#Get-AzureRmVM
#Get-AzureRmVM | Where-Object {$_.Tags.StartTime -eq '9am'}
$AzVMs = Get-AzureRmVM | Select-Object -Property Name, ResourceGroupName

foreach ($VM in $AzVMs)
{
    $VMName = $VM.Name
    $ResourceGroupName = $VM.ResourceGroupName

    Write-Host "Virtual Machine: $VMName"
    Write-Host "ResourceGroup  : $ResourceGroupName"
    Write-Host " "
}


Schedule Start and Stop of Azure VM

In this tutorial, I will show you how to automatically start and stop vm in azure.

We can achieve the automation Schedule Azure Virtual Machine Start/Stop with Azure Automation Runbooks and Adding Resource Tags to Azure VM.

Procedure/Approach:

  • Create an Azure VM or use any existing VM in Azure.
  • Assigning start and stop tags to a VM.
  • Create a Azure Automation Account with "Run as Account" or use an existing Automation Account.
  • Creating start and stop powershell workflow runbooks.
  • Testing the start and stop runbook scripts.
  • Creating a schedule time for Azure VM start and stop.
  • Linking scheduled tasks to an automation runbook.

Implementation:

Step 1: Create a basic Azure VM or use an existing Azure VM of your choice. 

Here, I will be using an existing Azure VM for demo.

Step 2: Select your target virtual machine and assign StartTime and StopTime tags.

Example: StartTime = 9:00am; StopTime = 9:00pm




Step 3: Create an Azure Automation Account Run as Account

Step 4: Install Az Modules from Azure Automation Account Gallery

Step 5: Create a powershell workflow runbook.

## Schedule Azure VM Startup
workflow Cloud-Ops-StartTime-Test-Runbook 
{
   inlineScript {
     $azConn = Get-AutomationConnection -Name 'AzureRunAsConnection'
        
     # Add the automation account context to the session
     Add-AzureRMAccount `
        -ServicePrincipal `
        -Tenant $azConn.TenantID `
        -ApplicationId $azConn.ApplicationId `
        -CertificateThumbprint $azConn.CertificateThumbprint
        
     # Get the Azure VMs with tags matching the value '9:00am'
     $azVMs = Get-AzureRMVM | Where-Object {$_.Tags.StartTime -eq '9:00am'}

     # Start Azure VM/VMs
     $azVMS | Start-AzureRMVM
   }
}




## Schedule Azure VM Shutdown
workflow Cloud-Ops-StopTime-Test-Runbook {
   inlineScript {
     $azConn = Get-AutomationConnection -Name 'AzureRunAsConnection'
        
     # Add the automation account context to the session
     Add-AzureRMAccount `
        -ServicePrincipal `
        -Tenant $azConn.TenantID `
        -ApplicationId $azConn.ApplicationId `
        -CertificateThumbprint $azConn.CertificateThumbprint
      
     # Get the Azure VMs with tags matching the value '9:00pm'
     $azVMs = Get-AzureRMVM | Where-Object {$_.Tags.StopTime -eq '9:00pm'}           
        
     # Start Azure VM/VMs
     $azVMS | Stop-AzureRMVM -Force
   }
}