question

Krishna-0852 avatar image
0 Votes"
Krishna-0852 asked AndreasBaumgarten commented

Azure - Disk information


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-virtual-machinesazure-cloud-servicesazure-disk-storage
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

shivapatpi-MSFT avatar image
1 Vote"
shivapatpi-MSFT answered Krishna-0852 commented

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.


image.png (45.1 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

It worked... Thank you so much for the help..

0 Votes 0 ·
AndreasBaumgarten avatar image
3 Votes"
AndreasBaumgarten answered AndreasBaumgarten commented

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

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

It worked... Thank you so much for the help..

0 Votes 0 ·

Hi @Krishna-0852 ,


If you found the answer helpful, it would be great if you please mark it "Accept as answer". This will help others to find answers in Q&A

----------
Regards
Andreas Baumgarten

1 Vote 1 ·