Last week during another Microsoft Azure Camp someone asked, How do I ensure that i can stretch that “Free Trial”? As you may know, you can get a one month free trial of Azure. That free trial is really $220 OR 30 days witch ever comes first.
So I thought I would look at how we can Automate the automatic shutdown and deallocation of the VMs in my environment.
If automation is something you are interested in, please start with the following MVA online course:
Before we start ensure that your environment is prepared to use Runbooks. you can start with Step-by-Step: Get Ready to use Microsoft Azure Automation to prep your environment. Once the environment is ready.
Go to the portal, Select Automation, and open the Automation Account that you already have created using the steps in the above mentioned article.
Select RUNBOOKS in the menu
In the RUNBOOK page, Select New, App Services, Automation, Runbook, Quick Create. and enter the required info to create the new Runbook.
And click Create
Once created, select it and click Author in the menu.
In the authoring windows paste the following script. (Please be sure to change the red parts with the relevant info from your environment)
# Grab the management certificate
$cert = Get-AutomationCertificate –Name ‘aut-cert’
# Specify Microsoft Auzre SUBSCRIPTION ID and Name
$subID = '<Enter your Subscription ID Here>'
$subName = '<Enter your Subscription Name Here>'
# Set and Select the Azure SUBSCRIPTION
Set-AzureSubscription -SubscriptionName $subName -Certificate $cert -SubscriptionId $subID
Select-AzureSubscription -SubscriptionName $subName
# shutdown Lab VMs
#list all VMs
$vmList = ('VM1','VM2','VM3')
$svcName = 'CloudServiceName'
For ( $vmCount = 0; $vmCount -lt $vmList.Count; $vmCount++)
$vm = Get-AzureVM -ServiceName $svcName -Name $vmList[$vmCount]
if ( $vm.InstanceStatus -eq 'ReadyRole' )
-ServiceName $vm.ServiceName `
-Name $vm.Name `
Once this is done, Click on Save and Publish
Now the script is ready to run, but we want to ensure that it runs every night in case i forgot to shutdown and deallocate my VM. (see Step-By-Step: Virtual Machine billing in Azure for more info). To do so, click on Schedule in the menu and “Link to a new schedule”.
Fill in the info. (We want a daily schedule that will kick start the script at 8:00pm (20:00) ) and click the checkmark
That’s it. now every night at 20:00, the script will start and shutdown VM1, VM2 and VM3 in the CloudServiceName so that i don't waste my resources.
There are a lot more resources for automation of Microsoft Azure at the address below in the Automation section: