Saturday, May 30, 2015

Export to Excel Microsoft Azure VM List


As of today there is no way on full management portal ( ) or on new Azure portal ( to export the list of Azure Virtual Machines to Excel. This blog talks about the same. Actually this is very keen requirement and I have seen lot of people in search of this functionality. This blog is an attempt to simplify their life a bit.
The advantage of Azure VM list export to excel is that you have this excel maintained in TFS or SharePoint to track the Azure environment resources. This simplifies the Azure resource management drastically when the number of VM’s present in an Azure subscription is in hundreds. I have seen my client’s Azure subscription wherein the number of VM’s was 120 and was not having user friendly names. Then every day they were facing challenge to understand the purpose of VM, who is the owner of it, till how long we wish to maintain it, which all need to be shutdown everyday in the late night and to be started everyday in the morning and so on. So I suggested them to have this tracked in an excel sheet. They liked the idea but again came back to me stating that, writing so many VM’s list manually in excel sheet is so boring. Can you please export these Azure VM’s list to excel and provide to us K.
I was thinking there has to be Export To Excel button on Azure portal but unfortunately there is none. So, if you need your Azure VM list to export to excel then the only answer is to write Azure Powershell commands and get it exported as .csv file.
The below is the powershell commands that cane provide you Azure VM list exported as CSV which you can then manually save as Excel sheet. So here we go!

Applicable Technology Stack

  1. Azure subscription with at least 1 Azure  VM’s provisioned in it.

Setting up the connection

Open the Powershell ISE as an administrator as shown below –

First run the command -

This open a pop up and asks you to enter the credentials of Azure subscription. Please enter subscription live Id and password. This will make current powershell window connected to Azure.

Setting up Subscription of Azure

It may be possible that one live ID account may have multiple azure subscription associated to it. Therefore we need to first select the subscription of Azure from which we wish to export the VM list by using below command –
select-azuresubscription -SubscriptionName "Your Azure Subscription Name Goes Here"

Enter your subscription name in above command. The subscription name can be found from management portal as shown below –

Get Azure VM’s and export the list

First let’s declare an array to hold the list of Azure VM’s.
$results = @() 

Then retrieve the list of VM using below command –
$vms = Get-AzureVM 

The variable $vms consist of all VM’s present in the selected subscription. Therefore we need to iterate over this list to retrieve important information from Azure VM’s and at the end append it to array we defined above.
Once added to array, simply export to Azure with pipe as below –
$results | export-csv -Path "Path to CSV goes here. Example, c:\AzureVMInfo.csv"

So the entire source code I have written in a function as below –
Function Export-AzureVMsToCSV
    $results = @()
    $vms = Get-AzureVM      

    foreach ($vm in $vms)
        $details = @{           
                HostName                  = $vm.HostName
                CloudServiceName    = $vm.DNSName
                Name                          = $vm.Name
                VirtualNetworkName = $vm.VirtualNetworkName
                IPAddress                   = $vm.IpAddress
                PublicIPAddress         = $vm.PublicIPAddress
        $results += New-Object PSObject -Property $details      
    $results | export-csv -Path "Path to CSV goes here. Example, c:\AzureVMInfo.csv"
    Write-Output "export succeeded"

This way the .csv file will get created at the specified location with all details. Then you can save it as excel manually by opening the .csv file in excel sheet itself.

Source Code

Entire source code of Azure VM export to excel is available on below location –
Hope this helps.

Friday, January 16, 2015

Azure Blob Snapshots using REST Api and Client Library

My article of Azure blob snapshots deep dive has been published. The article outlines various azure blob snapshots operations using client library and REST Api. The full source code is also available at bottom to download from GitHub. Here is the link –

Happy Snapshotting!!

Saturday, October 18, 2014

Multiple web applications in single cloud service web role with multiple SSL certificates and custom domains over https using Server Name Indication

I know the title of this post is quite big and hard to understand in first go. But I wanted to highlight the important aspect I am going to touch in the article and hence such a long name.
Ok, so here in this post I am going to demo how you can host multiple web applications in single cloud service. I know you may say that there are numerous posts on the same topic what is different in here? Yes the difference is there. You may find that most of the posts talks about hosting multiple web sites in single cloud service web role but they do not focus on running them on HTTPS and with custom domain name. Exactly this is what I am explaining in this post.
So in essence, we will see that, how can we host multiple web applications in single cloud service web role. Then we will see how the same applications can be hosted on HTTPS using SSL certificates in single cloud service web role. After that we will extend the talk to hosting same apps with custom domains using new feature of IIS 8.0 called as Server Name Indication (SNI).
Applicable software stack – VS 2013 and above, Azure SDK 2.3 and above, IIS 8.0 and above. [IIS 7 not supported].
Create required project in Visual Studio
This part is pretty straightforward implementation. To start with I created a simple cloud service project in VS2013 and added one web role in it. I renamed the web role to ManegementAppWebRole as shown below –

I changed the default.aspx UI text to just make it identify as Management App. Now I added another ASP.NET web project in the same solution. This will be my survey app. In this also I changed the default.aspx text to just make it identify as Survey app. So in all my projects structure is as follows –

Hosting multiple web applications in the same cloud service web role
Okay, so here I am representing a way by which you can have two web sites hosted in the same cloud service web role. In my case one of the applications is Management app web role itself and another is Survey web application which is not the part of cloud service. So I will run the survey app as part of Management app web role. To configure this full IIS capability can be leveraged. We have a special file in cloud service project know ServiceDefinition.csdef file. My current structure of .csdef file is as shown below –

The <Sites> tag is the key to make this possible. So I just added another site in .csdef file and provided the path of Survey web application. And provided the actual physical path of my survey app. Most importantly if you observe I have provided the host header for my survey app as “”. This is the main differentiator of the survey app running in the Management app web role. IIS do not allow to have more than one site listening on the same port without adding host header. We need to provide host header because both of our applications will be running on the same Endpoint port which is 80. Therefore there has to be some way by which IIS can understand the request of user need to be served by which application. Therefore we need host header. The complete structure of my .csdef file is as shown below –

Now in reality my host header site “” do not exists. Therefore to make survey app resolve to this URL I need to update “hosts” file of my machine. Hosts file is located at C:\Windows\System32\drivers\etc\hosts. I copied the file to desktop and opened in notepad and added an entry as highlighted below –
Then from desktop I replaced / copy-pasted the hosts file back to its location.
Now let’s run the project in debug mode by pressing F5 and it open the browser with management app with url similar to below – Here 60123 is port opened by my emulator, this will different in your case. Open another browser and cope the same url of management app and just replace by our host header name i.e. So final url in new browser window will be - bingo!! I see my management app and survey app running on the same port as shown below –

Let’s run the same implementation in Full IIS and Full emulator mode. This can be configured from the cloud service project properties. To open the properties of cloud service project, right click on Cloud service project and select Properties.
Note – With Azure SDK 2.4 FULL Emulator has been deprecated; however I guess you should be able to run the Full IIS with Emulator Express. This is what I think!!
Ok, now let’s run the project by pressing Ctrl+F5 [DO NOT RUN in DEBUG MOE!!] and here is what I see in browser and IIS –

As you can see, host header has been applied for my Survey Application. This is how we can run multiple web application in single cloud service web role.
Advantage – You are running the tow different web apps in the same IIS. In other words you are running two different web applications in same instance of Azure role and hence you save money. In case of 2 different roles for 2 different web app, you will be charged for role VM instances of 2 applications. In this case you are charged for only one role VM instances.
Now in next part we will see how we can run these web applications over HTTPS and custom domain. This means, I have custom domain mapped for survey as “” and now we will have custom domain name updated in hosts file for Management app as “” plus to run them on https and certificates.

Monday, August 25, 2014

Using SAS, renew SAS and REST API to Upload large files to Azure blob storage in parallel and async

First of all thanks for overwhelming response to my earlier blog post related to upload of large files to azure block blob in parallel and async.
At the bottom of this post you will find the link for downloading the code sample.
In current post I will extend the same code library to support REST Api to perform azure blob upload. Also, as a best practice you should always use SAS (Shared Access Signature) for performing any operation on blob storage. Therefore in current post I will extend the code to support SAS as well.
Why we need to renew or extend Azure SAS –
Using SAS with blob storage is great option to provide one more level of security to azure storage operations however, anyone who gets access to SAS url can perform malicious operations. Therefore keeping SAS expiry time to minimum is always a good practice. But again, if we are performing upload of fairly large files then keeping SAS expiry to minimum will not help.
For example, let’s consider a scenario. Let’s say I will upload a file of size 100MB in azure blob storage using REST Api and SAS. As a best practice and already stated in my previous blog post of uploading large files to azure block blob in parallel, the files to be uploaded to azure need to be sliced in chunks.
Now if I have set the expiry time to 5 minutes. Of course upload of 100MB size will not be completed in 5 minutes and hence my block upload will fail in between.
Solution -
To continue to upload large files to azure block blob seamlessly after SAS expiry time, I need to renew the SAS token again. This is what exactly I am doing in this blog post and code sample.
So essentially, I check the response status code of every block upload. If response status code Created means current block is uploaded and continue to next. If the response status code is received as 403 Forbidden means the upload of block is failed due to invalid SAS. This is where I mark the particular failed block as Not Added.  Once the loop is over, I retrieve list of failed blocks and perform the same operation with new SAS. To perform same operation of upload with new SAS I am using popular and basic concept you also might know called as RECUSRSIVE FUNCTION.