Azure - Disk information

Narayanan, Krishnaprasad 21 Reputation points
2021-10-29T20:25:06.267+00:00

Hi,

I looking for an option to extract/export the Disk name and the Vm name to which disk is attached.

In simple I need the following columns in the extract

  1. Disk name
  2. RG Name
  3. VM Name
  4. VM RG name (optional - if possible yes )

N.b: I need to collect the information for more than 400+ disks,so manually checking and collecting the information is a pain.

Pls suggest how can I get this info from Azure.

Thank you in advance for your help.

Azure Cloud Services
Azure Cloud Services
An Azure platform as a service offer that is used to deploy web and cloud applications.
628 questions
Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
6,983 questions
Azure Disk Storage
Azure Disk Storage
A high-performance, durable block storage designed to be used with Azure Virtual Machines and Azure VMware Solution.
563 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Andreas Baumgarten 94,196 Reputation points MVP
    2021-10-29T21:29:03.943+00:00

    Hi @Krishna-0852 ,

    I would recommend to use the AZ PowerShell cmdlets instead of the older AzureRM cmdlets.
    here you go:

    $result = "DiskName,DiskResourceGroupName,VMname,VMResourceGroupName `r`n"  
    $diskObjects = get-azdisk  
    foreach ($diskObj in $diskObjects) {  
        $diskName = $diskObj.Name      
        $diskRG = $diskObj.ResourceGroupName  
        $relVMname = ($diskObj.ManagedBy).Split("/")[-1]  
        $VMobj = Get-AZVM -Name $relVMname  
        $vmName = $VMobj.Name  
        $vmRG = $VMobj.ResourceGroupName  
        $result += "$diskName" +","+ "$diskRG" +","+ "$vmName" +","+ "$vmRG" + "`r`n"  
        }  
    $result  
    

    The $result variable contains all the 4 details you asked for.

    I added a "shorter" version with less lines here: https://github.com/abaumgarten42/Useful_PSscripts/blob/main/Get-AzureDiskAndRelatedVMs/Get-AzureDiskAndRelatedVMs.ps1

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    3 people found this answer helpful.

  2. shiva patpi 13,046 Reputation points Microsoft Employee
    2021-10-29T20:50:10.807+00:00

    Hello @Krishna-0852 ,
    Can you try the command:-
    Get-AzureRMDisk | select ResourceGroupName , Name , ManagedBy

    Above command will give you the Disk Name , Resource Group Name & the VM Name (Column Managed By)

    144986-image.png

    You can also export using the same command to .csv file
    Get-AzureRMDisk | select ResourceGroupName , Name , ManagedBy | Export-Csv -Path test.csv

    ManagedBy column will give you the whole path of the VM Name which it is using. Once you export to .csv file you can write a simple excel formula to extract only the VM Name.

    If the above answer helps out , kindly make sure to "Upvote & Accept the answer"

    Regards,
    Shiva.

    1 person found this answer helpful.