Retrieve Azure VM details by using PowerShell.

Recently I built a large environment in Azure with more than 1000 Virtual machines. Now we need to provide the Virtual Machine details to the customer(or raise the SNOW ticket) and it is very difficult to collect each VM detail manually from Azure Portal. Also, there can be another use case if you want to verify the VMs to compare with each other to ensure all the VMs are created the same way. For example Cache setting for all the VMs should be Read /Write. You may also want to grab details of all the data disks and OS disks and their size, name info and cache settings. This script grabs all the info in one shot and exports it into a CSV file for further manipulation. This script also provides Private and Public IP addresses.

# Connect to Azure Account
Connect-AzAccount 

# Create Report Array
$report = @()

# Record all the subscriptions in a Text file  
$SubscriptionIds = Get-Content -Path "c:\inputs\Subscriptions.txt" 
Foreach ($SubscriptionId in $SubscriptionIds) 
{
$reportName = "VM-Details.csv"

# Select the subscription  
Select-AzSubscription $subscriptionId
 
# Get all the VMs from the selected subscription
$vms = Get-AzVM
 
# Get all the Public IP Address
$publicIps = Get-AzPublicIpAddress
 
# Get all the Network Interfaces
$nics = Get-AzNetworkInterface | ?{ $_.VirtualMachine -NE $null} 
foreach ($nic in $nics) { 
    # Creating the Report Header we have taken maxium 5 disks but you can extend it based on your need
    $ReportDetails = "" | Select VmName, ResourceGroupName, Region, VmSize, VirtualNetwork, Subnet, PrivateIpAddress, OsType, PublicIPAddress, NicName, ApplicationSecurityGroup, OSDiskName,OSDiskTier, OSDiskCaching, OSDiskSize, DataDiskCount, DataDisk1Name, DataDisk1Tier, DataDisk1Size,DataDisk1Caching, DataDisk2Name,DataDisk2Tier, DataDisk2Size,DataDisk2Caching, DataDisk3Name, DataDisk3Tier, DataDisk3Size,DataDisk3Caching,  DataDisk4Name, DataDisk4Tier, DataDisk4Size,DataDisk4Caching, DataDisk5Name,DataDisk5Tier, DataDisk5Size,DataDisk5Caching
   #Get VM IDs
    $vm = $vms | ? -Property Id -eq $nic.VirtualMachine.id 
    foreach($publicIp in $publicIps) { 
        if($nic.IpConfigurations.id -eq $publicIp.ipconfiguration.Id) {
            $ReportDetails.PublicIPAddress = $publicIp.ipaddress
            } 
        } 
        $ReportDetails.OsType = $vm.StorageProfile.OsDisk.OsType 
        $ReportDetails.VMName = $vm.Name 
        $ReportDetails.ResourceGroupName = $vm.ResourceGroupName 
        $ReportDetails.Region = $vm.Location 
        $ReportDetails.VmSize = $vm.HardwareProfile.VmSize
        $ReportDetails.VirtualNetwork = $nic.IpConfigurations.subnet.Id.Split("/")[-3] 
        $ReportDetails.Subnet = $nic.IpConfigurations.subnet.Id.Split("/")[-1] 
        $ReportDetails.PrivateIpAddress = $nic.IpConfigurations.PrivateIpAddress 
        $ReportDetails.NicName = $nic.Name 
        $ReportDetails.ApplicationSecurityGroup = $nic.IpConfigurations.ApplicationSecurityGroups.Id 
        $ReportDetails.OSDiskName = $vm.StorageProfile.OsDisk.Name 
        $ReportDetails.OSDiskSize = $vm.StorageProfile.OsDisk.DiskSizeGB
        $ReportDetails.OSDiskCaching = $vm.StorageProfile.OsDisk.Caching
        $ReportDetails.DataDiskCount = $vm.StorageProfile.DataDisks.count
        $ReportDetails.OSDiskTier = ((Get-AzDisk -ResourceGroupName $vm.ResourceGroupName  -DiskName $vm.OsDisk.Name).Tier | Out-String).Trim()

        if ($vm.StorageProfile.DataDisks.count -gt 0)
        {
         $disks= $vm.StorageProfile.DataDisks
     foreach($disk in $disks)
        {
        If ($disk.Lun -eq 0)
        {
       $ReportDetails.DataDisk1Name = $vm.StorageProfile.DataDisks[$disk.Lun].Name 
       $ReportDetails.DataDisk1Size =  $vm.StorageProfile.DataDisks[$disk.Lun].DiskSizeGB 
       $ReportDetails.DataDisk1Caching =  $vm.StorageProfile.DataDisks[$disk.Lun].Caching 
       $ReportDetails.DataDisk1Tier = ((Get-AzDisk -ResourceGroupName $vm.ResourceGroupName -DiskName $vm.StorageProfile.DataDisks[$disk.Lun].Name).Tier | Out-String).Trim()
        }
        elseif($disk.Lun -eq 1)
        {
        $ReportDetails.DataDisk2Name = $vm.StorageProfile.DataDisks[$disk.Lun].Name 
        $ReportDetails.DataDisk2Size =  $vm.StorageProfile.DataDisks[$disk.Lun].DiskSizeGB 
        $ReportDetails.DataDisk2Caching =  $vm.StorageProfile.DataDisks[$disk.Lun].Caching 
        $ReportDetails.DataDisk2Tier = ((Get-AzDisk -ResourceGroupName $vm.ResourceGroupName -DiskName $vm.StorageProfile.DataDisks[$disk.Lun].Name).Tier | Out-String).Trim()
        }
        elseif($disk.Lun -eq 2)
        {
        $ReportDetails.DataDisk3Name = $vm.StorageProfile.DataDisks[$disk.Lun].Name 
        $ReportDetails.DataDisk3Size =  $vm.StorageProfile.DataDisks[$disk.Lun].DiskSizeGB 
        $ReportDetails.DataDisk3Caching =  $vm.StorageProfile.DataDisks[$disk.Lun].Caching 
        $ReportDetails.DataDisk3Tier = ((Get-AzDisk -ResourceGroupName $vm.ResourceGroupName -DiskName $vm.StorageProfile.DataDisks[$disk.Lun].Name).Tier | Out-String).Trim()
        }
        elseif($disk.Lun -eq 3)
        {
        $ReportDetails.DataDisk4Name = $vm.StorageProfile.DataDisks[$disk.Lun].Name 
        $ReportDetails.DataDisk4Size =  $vm.StorageProfile.DataDisks[$disk.Lun].DiskSizeGB 
        $ReportDetails.DataDisk4Caching =$vm.StorageProfile.DataDisks[$disk.Lun].Caching 
        $ReportDetails.DataDisk4Tier = ((Get-AzDisk -ResourceGroupName $vm.ResourceGroupName -DiskName $vm.StorageProfile.DataDisks[$disk.Lun].Name).Tier | Out-String).Trim()
        }
        elseif($disk.Lun -eq 4)
        {
        $ReportDetails.DataDisk5Name = $vm.StorageProfile.DataDisks[$disk.Lun].Name 
        $ReportDetails.DataDisk5Size =  $vm.StorageProfile.DataDisks[$disk.Lun].DiskSizeGB 
        $ReportDetails.DataDisk5Caching =  $vm.StorageProfile.DataDisks[$disk.Lun].Caching 
        $ReportDetails.DataDisk5Tier = ((Get-AzDisk -ResourceGroupName $vm.ResourceGroupName -DiskName $vm.StorageProfile.DataDisks[$disk.Lun].Name).Tier | Out-String).Trim()
        }
       }
        }
        $report+=$ReportDetails 
    } }
     
$report | ft -AutoSize VmName, ResourceGroupName, Region, VmSize, VirtualNetwork, Subnet, PrivateIpAddress, OsType, PublicIPAddress, NicName, ApplicationSecurityGroup, OSDiskName, OSDiskTier, OSDiskCaching, OSDiskSize, DataDiskCount, DataDisk1Name, DataDisk1Tier, DataDisk1Size,DataDisk1Caching, DataDisk2Name,DataDisk2Tier, DataDisk2Size,DataDisk2Caching, DataDisk3Name, DataDisk3Tier, DataDisk3Size,DataDisk3Caching,  DataDisk4Name, DataDisk4Tier, DataDisk4Size,DataDisk4Caching, DataDisk5Name,DataDisk5Tier, DataDisk5Size,DataDisk5Caching

#Change the path based on your convenience
$report | Export-CSV  "c:\outputs\$reportName"

I hope this helps.

6 Comments

      1. How would you do this? I would be very interested. I have tried things such as Get-AzMetric, but I am having no luck!

        Like

  1. Hi Rajaniesh,
    The above script is working like a magic. Thanks for helping me and please keep posting.My manual work got stopped becoz of this script. Thanks again Rajaniesh !!!!

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.