the result

I regulalry need to check if my Azure virtual machines are turned off before leaving. Besides virtual machines, I also want to know in which pricing level my web sites, Azure SQL Databases, etc are running. I want to know if I have an HDInsight cluster running.

So I created an Azure automation job that checks the subscriptions at 6pm every day.

Here is how it looks:




How to start

You can start using Azure automation by following the instructions available here:


The script will need to get access to the subscriptions.

So I created a management certificate. One way to do so is explained in this blog post by Keith Mayer.

In my case, here is how my environment looks:


In Azure automation, the same certificate is declared in the assets:


The script

Here is how the script itself:

workflow Inventory
    # Get the Azure management certificate that is used to connect to this subscription
    $Certificate = Get-AutomationCertificate -Name ''
    if ($Certificate -eq $null)
        throw "Could not retrieve '$AzureConn.AutomationCertificateName' certificate asset. Check that you created this first in the Automation service."

        $Certificate = $using:Certificate

        $subscriptions = (('Azdem169A44055X','0fa8xxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'), 
        ('Azure bengui','b4edxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'), 
        foreach ($subscription in $subscriptions)
            $subscriptionName = $subscription[0]
            $subscriptionId = $subscription[1]
            echo "------- Subscription $subscriptionName ----------"

            # Set the Azure subscription configuration
            Set-AzureSubscription -SubscriptionName $subscriptionName -SubscriptionId $subscriptionId -Certificate $Certificate
            Select-AzureSubscription -Current $subscriptionName 

            $vms = @()
            foreach ($s in Get-AzureService)
                $vms += Get-AzureVm -ServiceName $s.ServiceName
            echo "--- Virtual Machines ---"
            $vms | select servicename, Name, PowerState | format-table
            echo "--- HDInsight clusters ---"
            $hclusters | format-table
            echo "--- Web Sites ---"
            $webs | select Name, SiteMode |  sort Name | format-table
            $dbs = @()
            foreach ($s in Get-AzureSqlDatabaseServer)
                $dbs += Get-AzureSqlDatabase -ServerName $s.ServerName
            echo "--- SQL Databases ---"
            $dbs | select Name, Edition, MaxSizeinGb | format-table    

NB: I have obfuscated the subscription ids.

Make it your own

You can change the script for your own usage. You would need to change the certificate name (mine is, the names and ids of your subscriptions).



Benjamin (@benjguin)