Getting a report for all VMs and it's related OS and Data Disks - Code Sample

@20aman    Jul 15, 2021

In the past, I wrote a script sample to output all VMs related reports. Now, this report is easily available in the Azure portal when you are viewing the VMs. You can even apply any filters and add/remove any columns for more data. Then you can download the csv for the data you see.

One thing that is not available is a report of VMs and their related OS and Data disks. You may need this sometime as per your requirements. In this post, I am sharing one such script that pulls a report around all VMs and their all related disks.

Script location

The latest version of the script can be found in GitHub here: Get-AzResourceInfo-VMsAndAllRelatedDisks

A sample output CSV file and a sample excel report (based on the output csv) are also located at the same location.

Script Working

The script selects all subscriptions and iterates over those. Then within each subscription, it fetches all Resource Groups and iterates over those. The script also indicates a way for filtering over Resource Groups (while fetching these) by using wildcards.

For each Resource Group, it fetches all resources and iterates over these. Then it filters out the resources of type virtual machines. Specifically, it uses the filter to match the type to "Microsoft.Compute/virtualMachines".

$resource.Type -eq "Microsoft.Compute/virtualMachines"

Then for every VM, it fetches three different types of information and adds each as a separate row to the output CSV:

  1. The VM itself
  2. OS disk details
  3. Data disk details - one-row entry for each data disk

The script uses a value/column for "attachedToVM" for all types of rows to indicate which VM the entry belongs to. The script also outputs "ResourceType" column that you can further use to filter data.

Now that you have the data, you can create custom reports as per your requirements. One such report is provided in the same location for your easy reference.

If you have any improvements you would like to see in the script feel free to provide that as a comment here or a pull request on GitHub.





Comments powered by Disqus