Saturday, May 30, 2015

Export to Excel Microsoft Azure VM List

Update

This script talks about exporting classic VMs to Azure. To export Azure ARM VMs to CSV with PowerShell refer to new link below. This refers to all latest source code and technologies -  - https://sanganakauthority.blogspot.com/2019/09/azure-vms-export-to-csv.html




Introduction


As of today there is no way on full management portal ( http://manage.windowsazure.com ) or on new Azure portal (http://portal.azure.com) 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 -
Add-AzureAccount

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"
}
Export-AzureVMsToCSV

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 –
https://gallery.technet.microsoft.com/Export-to-excel-Azure-19930e31
Hope this helps.
Cheers…

No comments:

Post a Comment