Azure Power Shell: Azure Virtual Network for the Command Line Junkie – Part 4.1

Web UI looks good and pretty but if you really want to get work done at scale, manage, automate and administer: command line is your way. As a command line junkie from the UNIX world I thought of exploring what Azure PowerShell had to offer. And boy, I was quite blown by the ease of use, functionality and flexibility it comes with. Through this tutorial series, I will take you through various scenarios and functionality on Azure that is made easy using Azure Power Shell


Till now, I have covered the following services in Azure and how we use Power Shell to manage those:

Azure Powerhell: Azure Websites for the command line junkies! – Part 1
Azure Power Shell: Azure Virtual Machines for the command line junkie! - Part 2
Azure Power Shell: Azure Cloud Services for the Command Line Junkie -Part 3

In this post, I will cover some of the basic concepts in Azure Virtual Network and how it can be managed using PowerShell. Virtual Network is an important design element for your environment. Right from setting up your network, to implementing subnets, virtual machines and cloud services within your network, this post will get you started to have your network setup.

Creating the VNET

The way to create a VNET in Azure can be accomplished through the management portal or the preview portal or using PowerShell. When using Powershell, we need to have a xml .netcfg file that can be used to add the requisite configuration for the virtual network. One way to get a format of the xml template is to import it from your Azure Subscription if you already have a vnet created.

   1: #Add the Azure account to your powershell session
   2: Add-azureaccount
   4: #Select Subscription
   5: Select-AzureSubscription -SubscriptionName "Visual Studio Ultimate with MSDN"
   7: #Import the .netcfg from the subscription account
   8: Get-AzureVNetConfig -ExportToFile C:\Users\addatta\Desktop\myazurenetcfg.netcfg

The XML .netcfg file may look something like this. (Note: I have created a very basic network in my subscription and hence most of the network elements are missing)

   1: <?xml version="1.0" encoding="utf-8"?>
   2: <NetworkConfiguration xmlns:xsd="" xmlns:xsi="" xmlns="">
   3:   <VirtualNetworkConfiguration>
   4:     <Dns />
   5:     <VirtualNetworkSites>
   6:       <VirtualNetworkSite name="Mars_VNET" Location="East US">
   7:         <AddressSpace>
   8:           <AddressPrefix></AddressPrefix>
   9:         </AddressSpace>
  10:         <Subnets>
  11:           <Subnet name="Subnet-1">
  12:             <AddressPrefix></AddressPrefix>
  13:           </Subnet>
  14:         </Subnets>
  15:       </VirtualNetworkSite>
  16:     </VirtualNetworkSites>
  17:   </VirtualNetworkConfiguration>
  18: </NetworkConfiguration>

Now this file can be modified as per your configuration. However, a more holistic detail of the VNET configuration schema can be found here. Configure a virtual network using the network configuration file details this process further.

Once we have this network configuration file, we can use the Set-AzureVNetConfig to create our virtual network:

   1: Set-AzureVNetConfig -ConfigurationPath C:\MyAzureNetworks.netcfg

On the same lines we can also modify the configuration of the network.

Adding a Virtual Machine to your network

There are a couple of ways that a virtual machine can be added to a network. During creation time, the VNET may be specified for the virtual machine. Once a VNET is fixed for a cloud service, all the VMs in the cloud service are assigned the same VNET. The 2 ways to create VNETS using PowerShell are by using the NewAzureQuickVM and New-AzureVM cmdlet as shown below:

   1: #using NewAzureQuickVM
   3: $vnet = "mars_vnet"
   4: $subnet = "subnet-1"
   5: $ServiceName = "MyCloudService"
   6: VMName = "MyWinVM1"
   7: ImageName = "<some windows image name>"
   9: NewAzureQuickVM -Windows -ServiceName $ServiceName -Name $VMName $ImageName -AdminUserName "adarsha" -Password "<password>" -Location "East US" `
  10: -InstanceSize $size -SubnetName $subnet -VNETName $vnet
   1: $VNetName = "Mars_VNET"
   2: $Subnet = "Subnet-1"
   3: $StaticIP = ""
   5: New-AzureVMConfig -Name $VMName -InstanceSize $size -ImageName $image |
   6: Add-AzureProvisioningConfig -Windows -AdminUserName "adarsha" -Password "<password>" |
   7: Set-AzureStaticVNetIP -IPAddress $staticIP |
   8: Set-AzureSubnet -SubnetNames $subnet |
   9: New-AzureVM -ServiceName $ServiceName -Location "East US" -VNetName $vnet

In case of cloud services, once the cloud service is created, you will have to add the VirtualNetworkSite element in the NetworkConfiguration element of the .csfg file of the cloud service before deploying it.

Configure Internal Load Balancer

When creating virtual machines in a virtual network, it is sometimes important to have a load balanced set of the virtual machines. Using powershell it can be done during the creation process of the virtual machines. The process is as shown below:

   1: #Create the Load ballanced object
   2: $vip = ""
   3: $lbname = "my_load_ballancer"
   4: $subnet = "subnet_1"
   5: $vnet = "my_vnet"
   7: #Create the load ballancer configuration object
   8: $ilb = New-AzureInternalLoadBalancerConfig -InternalLoadBalancerName $lbname -StaticVnetIPAddress $vip -SubnetName $subnet
  10: #Create the Virtual machine objects 
  11: $vm1 = New-AzureVMConfig -ImageName $ImageName -Name "VmLB1" -InstanceSize "Small" |
  12: Add-AzureProvisioningConfig -Windows -AdminUserName "adarsha" -Password "<password>" |
  13: Set_azureSubnet -SubnetName $subnet |
  14: Add-AzureEndpoint -Name "web" -Protocol tcp -LocalPort 80 -PublicPort 80 -LBSetName "weblbset" -InternalLoadBalancerName $lbName -DefaultProbe
  16: $vm2 = New-AzureVMConfig -ImageName $ImageName -Name "VmLB2" -InstanceSize "Small" |
  17: Add-AzureProvisioningConfig -Windows -AdminUserName "adarsha" -Password "<password>" |
  18: Set_azureSubnet -SubnetName $subnet |
  19: Add-AzureEndpoint -Name "web" -Protocol tcp -LocalPort 80 -PublicPort 80 -LBSetName "weblbset" -InternalLoadBalancerName $lbName -DefaultProbe
  21: #Now create the virtual machines with the New-AzureVM cmdlet which is already assigned the load balanced set
  22: New-AzureVM -ServiceName $cloudservice -Location $location -VNetName $vnetName -VMs $vm1, $vm2 -InternalLoadBalancerConfig $ilb


In this section, I basically covered how do we get started with creating a Virtual network using PowerShell. I have not covered some of the more key and important concepts of Hybrid and Multi site network such as Site-Site and Point-Site and virtual network to virtual network. I will cover these key topics and such important concepts in the following post. Stay tuned!

Comments (0)

Skip to main content