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:

http://azure.microsoft.com/en-us/documentation/articles/automation-create-runbook-from-samples/

Credentials

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 'azure-admin.3-4.fr'
    if ($Certificate -eq $null)
    {
        throw "Could not retrieve '$AzureConn.AutomationCertificateName' certificate asset. Check that you created this first in the Automation service."
    }

    InlineScript
    {
        $Certificate = $using:Certificate

        $subscriptions = (('Azdem169A44055X','0fa8xxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'), 
        ('Azure bengui','b4edxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'), 
        ('APVX','0ec1xxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'), 
        ('demos-frazurete','4b57xxxx-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
            
            $hclusters=Get-AzureHDInsightCluster
            echo "--- HDInsight clusters ---"
            $hclusters | format-table
        
            $webs=Get-AzureWebSite
            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 azure-admin.3-4.fr), the names and ids of your subscriptions).

 

Benjamin (@benjguin)

Blog Post by: Benjamin GUINEBERTIERE