Manage Azure IaaS with Windows PowerShell: Part 1

Doctor Scripto

Summary: Establish a connection to Azure through Windows PowerShell.

Honorary Scripting Guy, Sean Kearney, is here. I'm back from the MVP Summit with my head in the clouds! More specifically, the Azure sky!

Yes, my head is truly in the clouds. About a month ago, I got some training in Microsoft Azure and I got to meet the fellow who created the Windows PowerShell cmdlets for Azure, Michael Washam.

I looked up at him and began blubbering, "…I'm not worthy….I'm not worthy… I'm not worthy…"

Of course, Michael looked at me and was thinking, "Uh…OK…yeah…So let's get into creating some site-to-site networking!"

Well, it wasn't quite like that (other than me bowing and prostrating like some silly person of course)

But what I did learn during the course (between passing notes in class) was how to manage aspects of the Internet as a Service (IaaS) component of Azure with Windows PowerShell. This week, I'd like to share some of that with you.

If you've never played with IaaS, consider it as an incredibly powerful Hyper-V solution in a remote datacenter. There's a lot more to it than that, of course, but you can break it down to something as simple as that for understanding purposes.

It also comes with some snazzy new cmdlets for managing that infrastructure, including creating, shutting down, and configuring those virtual machines. Although they provide similar functionality as their Hyper-V counterparts, they are quite different.

Where they are similar is in how they can align with your business processes for creating and managing environments. Your Azure IaaS script may look different than its Hyper-V counterpart, but it will provide the same ability to create a new virtual machine. You can even leverage Desired State Configuration (DSC) files in Windows PowerShell to define those virtual machines the same as you can with other physical machines and virtual machines.

Your first challenge is getting the cmdlets into your workstation and managing the environment in question. This topic on the Microsoft Azure site describes this process beautifully: How to install and configure Azure PowerShell.

We'll presume that you've gotten at least that far, and you are now staring up in the sky saying, "Fine, where do I start now?"

When you create virtual machines in Hyper-V, they need some way to communicate with each other. Normally, you'll access the Hyper-V Manager and create a virtual network in the GUI interface or run a simple Windows PowerShell command like the one that follows (in this example, we are creating a virtual network called "Dev" and configuring it as an internal network):

New-VMSwitch "Dev" –SwitchType Internal

As we create our virtual machines in Hyper-V, we reference the switch name to allow them to communicate with each other. Azure is no different in this aspect. We also need to create a network for the virtual machines to communicate.

There are some differences with Azure, of course:

  • Each network has a built in DHCP server, so you need to choose an IP range and scope.
  • Each network is located within a datacenter somewhere in the world, so you need to specify its location.
  • You must define a name for the network.

To get started, in the left pane of the Azure management portal, click NETWORKS, and then click the +NEW button:

Image of menu

We can choose Custom or Quick Create. For a basic network, we supply a name and choose a private IP address range and datacenter location for the network.

In our example, we name the network EOT-HSG-Network. This name is how we refer to our network—much like "Dev" was the switch name we used in our previous Hyper-V example.

Within Azure, there is a built-in DHCP server to ensure that systems always can obtain IP addresses. For this matter, we choose a private IP address range. In this example, we select 10.x.x.x with a CIDR of 16, which provides over 65,000 possibly addresses. Yes, we are a greedy lot today, aren't we?

Although I could choose North Europe for fun, I'm going to set up the network in East US. There is a built-in DNS server within this address for basic Internet resolution, but if your company uses a public-facing DNS server, you can add this in the DNS SERVER field.

Image of menu

When we are done, we will see a network in Azure that our virtual machines (and other Azure services) can communicate with.

Now of course, you're wondering, "Hey! Where's the Windows PowerShell in all of this?"

To this end, I am going to present you with a sample Windows PowerShell command that does all of this. The bad part is that this command involves XML (stop screaming!). The good part is that it is very easy to use.

We have two cmdlets to help us out.

The first one is Get-AzureVnetconfig, which allows us to back up the current network configuration for the entire Azure subscription:

Get-AzureVnetConfig –exporttofile C:\filename.xml

Our previous network configuration looks like this in XML:

Image of code

If for any reason, we deleted the network and had to re-create it (or you switched to a new Azure subscription and had to re-create it), we would simply run this cmdlet with the previously saved file:

Set-AzureVnetConfig –configurationpath C:\filename.xml

It's actually quite easy to add an additional network. Simply copy and paste the XML block starting from

<VirtualNetworkSite name and ending in </VirtualNetworkSite> and edit the field previously marked "EOT-HSG-Network" with the new name. Here's an example in which I created a second network in North Europe called "EOT-HSG-ElseWhere":

Image of code

To create this additional network, we run the Set-AzureVnetConfig cmdlet as we did previously:

Set-AzureVnetConfig –configurationpath C:\filename.xml

Pop on by tomorrow as we show you how to create a virtual machine within Azure!

I invite you to follow The Scripting Guys on Twitter and Facebook. If you have any questions, send an email to The Scripting Guys at, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, remember eat your cmdlets every day with a dash of creativity.

Sean Kearney, Windows PowerShell MVP and Honorary Scripting Guy


Discussion is closed.

Feedback usabilla icon