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/
Great post, (from Reddit here).
This is very useful to know, look forward to the second part of scheduling the stop/start actions.
Hi Daniel, thanks for your comment. Please see part 2 here, https://www.cloudinspired.com/azure-logic-apps-json-parse/
Enjoy!
Great script sir, thankyou very much,
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..
Thanks for your comment, glad its helping you!