Simple Stop & Start Azure VMs Powershell – Part 1/2

Use Azure to stop and start VMs the easy way using powershell and runbooks schedule. Part 1 is below and is all you need to stop and start VMs on a schedule using a quick setup and easy script (provided below).

Part 2 (to be released soon) will focus on using logic apps to read the Azure Automation output in Part 1 via a Parse JSON connector. This allow you to schedule the shutdown from the logic app and be more flexible by adding in other components like email alerting of VM status and other details in a simple way.

You would normally stop and start VMs on a schdule for your servers that are not required to be running 24/7 therefore saving money on compute costs. Be aware that storage costs still apply even when the VM is shutdown (deallocated). This is a great way to save money on Test VMs or VMs that are only used in the working day. We can schedule a VM shutdown overnight, for example 7pm then startup again in the morning at 7am, saving money on compute for a 12 hour period.

Full video showing how to stop and start VMs in Azure

Heres the Powershell code to copy and paste into your runbook

# Stop and start Azure Virtual Machines in Powershell
# Cloudinspired.com https://www.cloudinspired.com/simple-stop-start-azure-vms-powershell/

Param(
 [string]$VmName,
 [string]$ResourceGroupName,
 [ValidateSet("Startup", "Shutdown")]
 [string]$VmAction
)
 
# Login to Automation Account
$Conn = Get-AutomationConnection -Name AzureRunAsConnection
Add-AzureRMAccount -ServicePrincipal -Tenant $Conn.TenantID `
-ApplicationID $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint | Out-Null
 
# Startup Virtual Machines for example, VM01,VM02,VM03 etc
$vms = $VmName.split(',')
foreach($vm in $vms) {
IF ($VmAction -eq "Startup") {
    Start-AzureRmVM -Name $Vm -ResourceGroupName $ResourceGroupName | Out-Null
    #Write-Output "VM $Vm in Resource Group $ResourceGroupName was started Successfully" 
    $objOut = [PSCustomObject]@{
    ResourceGroupName = $ResourceGroupName
    VMName = $Vm
    VMAction = $VmAction
    }
    Write-Output ( $objOut | ConvertTo-Json)
    }
}

# Shutdown Virtual Machines for example, VM01,VM02,VM03 etc
$vms = $VmName.split(',')
foreach($vm in $vms) {
IF ($VmAction -eq "Shutdown") {
    Stop-AzureRmVM -Name $Vm -ResourceGroupName $ResourceGroupName -Force | Out-Null
    #Write-Output "VM $Vm in Resource Group $ResourceGroupName was stopped Successfully"
    $objOut = [PSCustomObject]@{
    ResourceGroupName = $ResourceGroupName
    VMName = $Vm
    VMAction = $VmAction
    }
    Write-Output ( $objOut | ConvertTo-Json)
    }
}

Part 2 is here https://www.cloudinspired.com/azure-logic-apps-json-parse/

 

About cloudinspired

Cloud Inspired authors have over 30 years experience within the IT industry, providing expertise and knowledge on infrastructure, hybrid, public and private clouds platforms. Detailed easy to follow technical videos, training and tutorial guides are provided by subject matter experts covering various technologies including Azure, IaaS, SaaS, PaaS and Microsoft 365. This website focuses mainly on the Microsoft 365 and Azure Cloud platform and provides easy to follow step by step technical guides, diagrams, cloud certifications and tutorials. The aim is to deliver articles and videos on Microsoft 365 and Azure Cloud from start to finish on many different Azure services and certifications, building and increasing the viewers knowledge in a short, logical, easy to understand format quickly getting to the point of the subject matter! Check out the YouTube channel for a full list of published Cloud Inspired videos and lets get inspired about Cloud!

View all posts by cloudinspired →

5 Comments on “Simple Stop & Start Azure VMs Powershell – Part 1/2”

  1. Great post… this is what I’m looking for my Azure training. It helps me to save time and money. THANKS for Sharing your knowledge with us..

     

Leave a Reply

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