May 26

Azure Demo : Batch Provision VM’s in thirteen data centers worldwide and manage them within 10 minutes!


When I got my Azure subscription, the first thing I was interested in was creating a VM somewhere else then in our data centers in Amsterdam and better yet in every available Azure data center in the world! This is not easy task to do via the portal or even with Azure PowerShell. Both requires you to setup the storage accounts and VM’s in every Azure region which involves a lot of work and administration. And of course as always my immediate thought was, let’s work it out and do that with some clever PowerShell automation. Little did I know that this project would take a while because of more and more requirements/features (and the interaction between them) were added along the way.

This finally resulted in a PowerShell tool/script which will let you batch provision and manage Azure VM’s within Microsoft-managed datacenters in thirteen regions around the world. All thirteen VM’s are ready to use within an astonishing ten minutes, go Azure PowerShell Automation! I tried to follow the latest PowerShell recommended practices and only abused ‘write-host’ for demo screen output interaction purposes. Security and validation are treated with the utmost regard. Therefore every change or high impact operation is tested, assessed and reviewed, thanks go out to Darryl van der Peijl.

Oh and did I already say that its now quite easy to register an Azure trial within five minutes, that said, you could start your very own CDN in just 15 minutes J


Demo bits


Azure Demo Provision VMs Globally

I posted a animated gif above of some of the key parts of the demo to immediately point out what it exactly is and does and of course to get you excited enough to read on J  If you just want to see the demo in action without any hassle than the YouTube clip link below is a good representation. VM creation speed may vary from machine to machine and performance of Azure at that moment. Read further down this post to get some more details to use the script and get familiar with the inner workings of it. Please let me know if you found a bug or have an improvement. Also feel free to use pieces of the code and experiment for yourself.

Script: https://gallery.technet.microsoft.com/Azure-Demo-Batch-Provision-b98c75db
Demo screencast: https://www.youtube.com/watch?v=mRkJmgg9IJ0
Execute script:  .\AzureDemo_ProvisionVMsGlobally.ps1
Help?: Get-Help  .\AzureDemo_ProvisionVMsGlobally.ps1 -Full



Of course you need to sign up with Azure, Azure offers a one month free trial in which you can spend €150 or $200 on all services. So you really want to keep your VM instance size ‘Small’ (script default) if you plan to keep the VM’s running for a while. 

Once you have a subscription you only need to download and install the Azure PowerShell Module which is officially maintained and available on Github. I recommend to download from over there, it’s a standalone package and only contains the module instead of a full Microsoft Web Platform Installer package containing all Azure software and all their dependencies.  Microsoft offers the full package for download here.

Experiencing some difficulties with the demo or want to test drive Azure for yourself then please follow the Azure PowerShell documentation here, it contains helpful insight to connect to Azure and get you started.

Please be aware of the Azure VM Core and Cloud Service limit in Azure (default 20), open up a support ticket with Microsoft when you get a PowerShell error about exceeding a limit. They will raise the limit dependent on your Azure service spending needs.

See: http://azure.microsoft.com/en-us/documentation/articles/azure-subscription-service-limits/#subscription-limits




This demo automatically deploys an Azure VM, Azure Service and related Storage Account in each available Microsoft Azure Datacenter. It also facilitates in the provisioning and management of these VM’s through the script advanced functions which are made available in the ‘Global’ scope and thus can be used in the console. This includes the VM and storage account provisioning, remote PowerShell session setup, PowerShell remote command execution, rdp connection and public endpoint update functions. This way it’s fairly easy to provision the VM’s in every Azure Datacenter available, use the resources (network/compute/storage) at that location, connect and execute any remote PowerShell commands.

Script input

All script parameters are optional and will be determined automatically when left blank, one of the goals was to keep user supplied information to a minimum. In addition to the configurable script parameters, the script’s main routine will prompt for the administrator credentials, the Windows image file (deployment) and in which region/datacenter the VM’s should be deployed. The VM and Storage Account names will form a name based on a unique prefix which you can supply as a script parameter. If you don’t supply one  ‘get-random’ is used to generate three numbers combined with DEMO in the string as a prefix. This is because the DNS name must be unique in Azure’s public cloud DNS namespace, *.cloudapp.net. I will try to separate the VM name and DNS name in the next version so you could name the VM’s whatever you want.  See the example below where all script parameters are used.

ImportPubFile switch is used to use the Azure certificate import cmdlet ‘Import-AzurePublishSettingsFile’  instead of the ‘Add-AzureAccount’ cmdlet method which expires after 12 hours. The ‘AddSubscription’ switch is used to add a subscription to work with when you already have a existing one. 

Console over GUI

The purpose of executing the script cmdlet based functions in the console instead of a GUI is to get acquainted with Azure PowerShell and play around with the functions or the Azure module cmdlets yourself. Everybody can click a cool and nifty IaaS solution together in the Azure portal, but what if something happens or you want to scale up? Did you document well enough and how fast do you have everything online again. That’s something to think about and what better way than do that with some cool Azure PowerShell automation!

More cool Azure PS stuff

Before you choose to clean up the demo with the ‘Cleanup-Demo’ function I highly recommend to use Mark Scholman’s Azure IaaS Toolkit to manage networking, load-balancing and firewalling between the VM’s. Dependent on the feedback I might try to integrate the script functions and main routine in a cool graphical interface from Sapien PowerShell Studio like Mark Scholman did. And to take the whole thing a step further what about integrating Azure’s Desired State Configuration or one even bigger step let’s throw Azure Resource Manager in the mix!

Nano on Azure

The only thing I’m really missing in this demo where I unfortunately have no control over is deploying the much anticipated Nano server Cloud OS. Windows Server Technical Preview 2 is already available to deploy and already a treat to test all the awesome announced new features on but of course we want to test/war drive this Cloud OS on Microsofts own Cloud Platform Azure and see how it performs and consumes. So when it’s out you’re also able to provision and manage Nano with this script.


Under the hood


The script and each function is fully documented therefore as always ‘get-help’ will show your way around. The begin block in the script itself loads all the functions/scriptblocks/workflows, checks for the 64-bit workflow environment and for the Azure module availability. The process block executes the main routine which invokes the functions needed for the VM and Storage Account creation. For demo purposes the routine explicitly asks which credential, deployment image and which datacenters/regions to use instead of specifying these as parameter values to the script itself. After validating and processing the information PS workflow is used to create the storage accounts and VM’s in parallel. Finally the end block validates and confirms the creation and outputs the management functions and their related examples to use. PowerShell workflow in version 3 is limited with a hardcoded maximum of five simultaneous threads, there are some workarounds and solutions (runspaces/jobs) to expand the number of threads but it’s wiser to be on the safe side and avoid running into any kind of restriction or thread limitation in Azure or Azure module.


When everything is up and running the ‘Execute-RemoteCommand’ facilitates in the commands and uses the ‘Setup-Session’ function to establish a connection to the VM and then executes a remote command against one or more VM’s. And last but not least the ‘ Generate-RDPFile’ generates a RDP file from the endpoint information and launches the RDP connection without any additional connection or authentication prompts. Additional VM’s in a particular region can be created with the ‘Create-AzureVM’ function. Everything is made super easy to use without prompts or any additional configuration. That said this script is made for a demo environment, in a production environment you have to review the code and see if it fits your needs, also from a security perspective point of view.

Functions and Code

Please use ‘Get-Help ‘function/script’ with the ‘-examples’ or ‘–full’ parameter to see what it exactly does and how you can invoke them.  Below are all the (cmdlet based) functions you can use after you executed the script.

Create-AzureVM : Creates an VM in an Azure location determined by the specified storage account.
Create-AzureStorageAccount : Creates an Storage Account in a given Azure data center location.
ConnectTo-Azure : Connects to the Azure subscription.
CleanUp-Demo : Deletes all VM’s and tied storage accounts created with the demo.
Retrieve-Image: Retrieves the latest Windows Image from Azure, this image is used to deploy the VM with.
Index-VMinfo : Retrieves and indexes (caches) storage account and VM information from Azure.
Setup-Session: Sets up a remote PSsession to the AzureVM.
Execute-RemoteCommand : Allows you to execute remote PS commands to the Azure VM.
Update-PublicEndpointPort: Updates the Azure Public Endpoint port for the given VM’s, the endpoint port increments with one for each additional VM.
Generate-RDPFile : Generates a RDP connection file.
Delete-AzureVM : Deletes one or more VM’s.
Delete-AzureStorageAccount : Deletes one or more storage accounts.
Select-VM : Select one or more script indexed Azure VM’s.

I recommend you to download the script from TechNet for the latest version but if you want to see the code then I happily invite you to click on the bar below expanding the Crayon code window and browse through almost 2K lines of readable refactored code. Don’t forget to read the feedback section before expanding J


I really, realllyyy want to have your feedback, let me know if you found a bug, have a improvement for me or if you just want to ask why I did specific things. I’ll update the script dependent on this reception and feedback so leave a comment or e-mail me! And while you’re at it follow me on Twitter! I promise, I won’t spam you with simple, useless or ‘old news’ tweets. Thank you for taking the time to read this post, I did my very best to keep it as compact, clear and simple as possible which was quite a challenge. J