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

workflow ShutDown-AllVMs {
    param (
        [parameter(Mandatory=$true)]
        [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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s