'Simplicity is the ultimate sophistication' - Leonardo da Vinci

Architecture

Posts

Clean-Up Your Cloud!

For all of you "prototype-ers" and "kick-the-tire-ers" the Azure Runbook and schedule  below will keep you Azure subscription clean and your bill low.  This article assumes you are familiar with setting up an Automation credential (if not look here).

workflow Shutdown-VMs
{
     Param
    (
        [Parameter (mandatory =$True,Position=1)]
        [string]$vmPrefix,

        [Parameter (Position=2)]
        [string]$subscriptionName ="Pay-As-You-Go",

        [Parameter (Position=3)]
        [string]$automationCredentialName ="autocred"
    )
    $Cred = Get-AutomationPSCredential -Name $automationCredentialName
   
    $null = Add-AzureAccount -Credential $Cred
    "Using $subscriptionName subscription"
    Select-AzureSubscription -SubscriptionName $subscriptionName -Current
    
   inlinescript
   {
        "Shutting down VMs starting starting with $Using:vmPrefix"
        get-azurevm | ? {$_.Name -like "$Using:vmPrefix*" -and $_.Status -eq "ReadyRole"} |
            Stop-AzureVM -Force
   }    
}

When you create the Azure Automation Schedule, you will be prompted for three parameters

  1. Automation Asset Credential Name
  2. The subscription that you'd like to use
  3. The prefix to filter the VM names against. The VMs in the subscription will be filtered, with the prefix being used to shutdown all of the VMs that start with the [vmPrefix] filter.

The Runbook schedule creation screen will look like this:

If you prefer to create the schedule in PowerShell: