I was recently working on a proof of concept in Azure for a client that needed a couple of VMs to test if Azure is a viable candidate for their on-premise workloads. The client only needed those VMs at certain hours on weekdays and that meant that I needed to implement a method to remove any unnecessary run-time costs and that’s where Azure Automation and PowerShell comes in 🙂

For this particular proof of concept I chose to deploy ARM VMs and that was a good decision because one of the key differences between Azure Resource Manager and Azure Service Manager is parallelization and that’s a major difference. For example purposes the Classic model doesn’t let you start, stop, deploy multiple VMs or any other Azure goodies at the same time and if you try to do that you get a generic error like the one quoted below.

The virtual machine ‘VMNAME’ operation failed: ‘Windows Azure is currently performing an operation with x-ms-requestid on this deployment that requires exclusive access.

Now that you know that the ARM model can do everything in parallel, you can now start writing PowerShell Workflows that allow you to use the -parallel parameter in foreach loops which basically allows you to run multiple tasks at once. So if you want to start 100 VMs or create 100 Storage Accounts in say 5 minutes, then that’s the way to go.

The scripts I wrote are to be considered as samples because they target Resource Groups which means that all the Virtual Machines in a Resource Group will start or stop, so please keep that in mind. The scripts can be adapted to target specific VMs without doing any hard coding in the scripts and that can be achieved by tagging the VMs which is another feature that ARM provides.

ARM Tags

Changing the scripts to look for tags is very simple. Basically you add another mandatory parameter and add a filter in the Get-AzureRMVM cmdlet as shown below:

All you need to do in order to make them work is to have an Azure Automation Account, an AAD user that’s co-administrator on the subscription to be configured as a credential asset in the automation account, the runbooks and a schedule or more.

Everything that I just mentioned can be done via the GUI or you can use PowerShell. I’ll add a extra script in this post that creates everything that you need to make them work but with one exception and that’s the creation of the AAD user and adding him to the subscription as co-admin. I chose to not include that part in the script because it requires an additional Azure Module to be installed and doing that for one single step is not worth it.

Here’s the script:

In order to run the scripts on your local machine, you need to have the latest Azure ARM PowerShell module which at the time of writing is at version 1.0.4. You can install the Azure ARM PowerShell module using PowerShell v5 if you’re already running Windows 10 by running Install-Module AzureRM and then Install-AzureRM cmdlets or you can get PowerShell V5 on older operating systems by installing WMF 5.
Windows Management Framework 5 can be installed on Windows Server 2012 R2, Windows Server 2012, Windows 2008 R2 SP1, Windows 8.1 and Windows 7 SP1 but at the time of writing this post WMF5 was pulled from the download center because of a bug that resets the PowerShell Module Environment to the default settings. You can read more about it here -> Blog post on WMF5 issue

Later Edit: WMF5 is back! Here’s the download link:

Now without further ado, here are the scripts 🙂

Merry Christmas!

Privacy Preference Center

    Pin It on Pinterest