Query to extract VM + Disk detaisl

Shashank Awasthi 20 Reputation points
2024-04-29T08:28:01.49+00:00

Hi,

I am looking for a resource graph query to extract following information - Subscription, Resource Group, VMs, VM size, Disk (Type/Size) and associated cost at each level.

Appreciate your feedback.

Regards,

Shashank

Azure Cost Management
Azure Cost Management
A Microsoft offering that enables tracking of cloud usage and expenditures for Azure and other cloud providers.
2,109 questions
0 comments No comments
{count} votes

Accepted answer
  1. SadiqhAhmed-MSFT 38,891 Reputation points Microsoft Employee
    2024-04-29T10:04:17.21+00:00

    Hello @Shashank Awasthi Thank you for posting your question on Microsoft Q&A platform. Happy to assist you!

    From the title of the question, I understand that you are looking for a resource graph query to extract following information - Subscription, Resource Group, VMs, VM size, Disk (Type/Size) and associated cost at each level.

    AFAIK, the usage report does not have any parameter on what disk is under which VM. So, you will have to manually do the mapping.

    However, I quickly did a search and found the below ARG query, personally not tried this, might help.

    resources

    | where type =~ 'Microsoft.Compute/virtualMachines'

    | extend vmSize = tostring(properties.hardwareProfile.vmSize)

    | extend diskType = tostring(properties.storageProfile.osDisk.osType)

    | extend diskSize = toint(properties.storageProfile.osDisk.diskSizeGB)

    | extend diskCount = arraylength(properties.storageProfile.dataDisks)

    | mv-expand disk=properties.storageProfile.dataDisks

    | extend diskType = strcat(diskType, '/', tostring(disk.managedDisk.storageAccountType))

    | extend diskSize = toint(disk.diskSizeGB)

    | project subscriptionId, resourceGroup, vmName = name, vmSize, diskType, diskSize, diskCount

    | join kind=leftouter (

    consumption

    | where type == "Microsoft.Consumption/costs"

    | summarize cost = sum(properties.pretaxCost), currency = max(properties.currency) by subscriptionId, resourceGroup

    ) on subscriptionId, resourceGroup

    | extend cost = iif(isempty(cost), 0, cost)

    | project subscriptionId, resourceGroup, vmName, vmSize, diskType, diskSize, diskCount, cost, currency

    | order by subscriptionId, resourceGroup, vmName asc

    This query retrieves all virtual machines in your Azure environment, along with their size, disk type, disk size, and disk count. It then joins this data with the consumption data to get the cost information for each virtual machine. The query also includes the subscription and resource group information for each virtual machine.

    Note that the cost information is based on the consumption data, which is updated daily and may not be up-to-date. Also, the cost information is an estimate and may not reflect the actual cost of the virtual machine.

    Hope this helps. Feel free to reply if you have any questions.


    If the response helped, do "Accept Answer" and up-vote it


0 additional answers

Sort by: Most helpful