Please note, this can be scheduled with azure automation

To set up creds to connect see Authenticating to Azure using Azure Active Directory

At TrueNorth we try to get the most out azure. We use it for Development, PreSales, Testing infrastructure architectures, Performance testing and more. It so easy to forget to turn a VM off and then you find yourself trying to navigate the site on your mobile to deallocate your VM.

So, we have created a PowerShell script that can be triggered at a set time, the script enumerates all your VMs and powers them down.

As we use our BizSpark subscription for non customer VMs it allows us filter with
Select-AzureSubscription BizSpark

[code language=”csharp” gutter=”false”]
workflow ShutDown-AllVMs {
param (
[String] $VMCredentialName = "YOUR ASSET NAME"

$Credential = Get-AutomationPSCredential -Name $VMCredentialName

if ($Credential -eq $null) {
throw "Could not retrieve ‘$VMCredentialName’ credential asset. Check that you created this asset in the Automation service."

Add-AzureAccount -Credential $Credential

Select-AzureSubscription BizSpark

InlineScript {
Get-azurevm | ? { $_.Status -eq "StoppedVM"} | Stop-AzureVM -Force
Get-azurevm | ? { $_.Status -ne "StoppedDeallocated"} | Stop-AzureVM -Force

# if we miss anything, this will not include VMs that are starting up
Get-azurevm | ? { $_.Status -eq "ReadyRole"} | Stop-AzureVM -Force

This is scheduled with the Windows Task Scheduler, it run at 8PM and will wake up the laptop if needed (it will go back to sleep). We could use the Scheduler feature of Azure but for the purpose of this post we are just going to trigger the PS script.

We are also looking into creating an Azure AddOn that will just deallocates your VM when its not in use after or at certain times.

Get our latest articles in your inbox

Enjoyed this article? Sign up for our email newsletter and get real-world information on all things Microsoft, cloud and tech. Your information will be shared with MailChimp but no one else, and you can unsubscribe with one click at any time