March 24, 2015
, , , , ,

Doing more than provisioning with Azure VM using PowerShell

by pug-admin in Microsoft Azure 0 comments

Most of us build VMs as soon as we get access to Azure Subscription and first thing we do is RDP to that VM and spend time there to understand what all things we can do over there.

Microsoft Azure brings openness to Azure Cloud Platform with exclusive support to Linux VMs and thus support to various other flavors of Open Source. Few of us use VMs without putting them in Virtual Network (VNET) for doing experiments or use VM as test environment to see what all things can run and supported.

Today we will see how we can quickly build Azure VMs with PowerShell cmdlets and what all things we can do with the VMs beside just vanilla VM Provisioning. We will see how can add endpoints, extension and additional Data Disks. To understand the various aspects.

VMs across subnets in VNET

Consider a scenario where you need to put couple of your VMs under a defined VNET which is created in your subscription. For the time being let’s not consider which approach will be taken to connect to this VNET (S2S or P2S). It is assumed that readers have already downloaded PowerShell for Windows Azure and set their subscription. First we will see what all Windows OS Images we have in the subscription, for this you can fire following PowerShell Command :

Note that when you fire this command you will see multiple labels are common. This is because Azure have multiple Images based on Date or latest updated release date. So you need to be careful while picking up image from here and ensure you are using latest image. To pick up the latest Image you can use following cmdlet. Below example cmdlet shows you how you can pick up latest Windows Server 2012 R2 Datacenter, similarly you can use for other major version of Windows.

This cmdlet queries over collection and give you a single unique OS Image name which is sorted by published date. If you write the output of above on your PowerShell prompt, you can see image is in form of .vhd file as shown below :

<some alphanumeric number>

To add VM in availability set ( For Fault-domain) you need to make sure that VMs are part of same cloud service. Cloud service can be created using following cmdlet

Post we need to create New VM and set Availability Set to the VM, for that you can use following cmdlet :

With this we have build the general VM Configuration, however to add it to a particular subnet and set particular IP (Internal IP) we need to add additional cmdlet as shown below :

Above cmdlet will do 3 things for us :

  1. Build VM with Windows OS and create Administrator User with the credentials we provide.
  2. Set the specified Subnet of VNET for this VM / put VM in given subnet of VNET
  3. Set Internal IP address for this VM. Note that by doing this, your VM will get associate with this IP. So in case of Deallocation or restarts, IP address will not change.

Azure Management Portal gives you pictorial representation of your subnets in the tabular form and let you know the available IP range which you can use to allocate VMs. Below Image shows how it shows, for security reasons, we have masked IP addresses, however you can see similar table in your subscription.

VNET and Subnet details
VNET and Subnet details

Ideally with this you can also associate Domain joining details like Domain name, Domain User and Password and DNS settings in order to complete the setup end to end. Domain joining can be done using Remote PowerShell or manually by doing RDP on the VM. Currently in this blogpost we are keeping Domain Joining out of scope. Now to create VM out of this configuration and start provisioning, you need to finally execute the following cmdlet :

You can combine all above cmdlets in single PowerShell to simply end to end execution. After this if you check on Management Portal/Ibiza Portal, you can see you VM is in provisioning state and it will then start and will be ready for any operations. By default PowerShell and RemoteDesktop endpoints are added to VMs. You can add more to it either from Management Portal or from PowerShell cmdlet shown below :

Now let’s see how we can add extensions to VMs, we will see how we can add default BGInfo extension and how we can enable Symantec Endpoint Protection on VM. For BGInfo you can use following cmdlet :

Similarly for Symantec Endpoint Protection you can use following cmdlet :

Note that the Symantec Endpoint Protection is valid upto 60 days only and you need to purchase license further in order to continue using it. Refer more information on Symantec Website. If you wan to see all the list of available extensions in Azure, you can use following cmdlet :

Get-AzureVMAvailableExtension | Out-GridView

This will show the list in a Grid format on a popup window shown below :

VM Extensions available in Grid View
VM Extensions available in Grid View


You can then validate the Extensions added via PowerShell on Management Portal in the extensions section of VM Dashboard like shown below :

VM Extensions on Azure Portal
VM Extensions on Azure Portal

Lastly we will see how to add additional Data Disks to your VM. For this you need to first check the official documentation provided by Microsoft Azure which list at what VM size how much disks and disk space can be added. Accordingly for your VM you need to limit the Disk size and number of Disks. Using below PowerShell cmdlet you can quickly add DataDisk to your existing VM :

Here LUN stands for Logical Unit Number and HostCaching either you can keep None or if you want you can chage to ReadOnly or ReadWrite. You need to set it according to your business needs.

By now you have understood the potential of PowerShell and how much flexibility it brings to you. Over Management Portal operations, you can achieve better automation and make your deployments of VM in cloud simple, fast and effective with minimal errors and risks. In this post we have talked how you can use various PowerShell cmdlets to empower your VM and achieve multiple requirements using single PowerShell script. You can generalized this and make it parameter based end to end. To accept parameters you can define / declare / initialize before running actual Azure PowerShell cmdlets like shown below :



Similarly you can add multiple parameters with different types like String, Integer. Hope this post will help you to automate your VM deployments and make it simple for your large deployments. In upcoming post, we will talk more on Domain Join and other aspects related to VM and VNET. So keep visiting this blog.

Hope you enjoyed this post.

-Vikram Pendse.

Leave a comment