文章“【Azure Developer】在Azure Resource Graph Explorer中查看当前订阅下的所有资源信息列表并导出(如VM的名称,IP地址内网/公网,OS,区域等) ”的另一个示例。
问题描述
使用Azure Resource Graph查询:把这订阅(Subscription ID)下的VM虚拟机和相关磁盘信息拉取一下数据(VM名字,VM的tag,VM的资源ID,硬盘名字,硬盘大小,磁盘的资源ID)
问题答案
根据Azure Resource Graph Explorer中提供的Resources表来查看VM的相关信息。而磁盘相关的信息处于 properties.storageProfile.osDisk 以及 properties.storageProfile.dataDisks中。
示例查询语句为:
resources | where type =~ 'microsoft.compute/virtualmachines' | extend osDisk = properties.storageProfile.osDisk, disks = properties.storageProfile.dataDisks | project name, tags, id, osDisk.name, osDisk.diskSizeGB, osDisk.managedDisk.id, disks[0].name, disks[0].diskSizeGB, disks[1].managedDisk.id, disks[1].name, disks[1].diskSizeGB, disks[1].managedDisk.id, disks[2].name, disks[2].diskSizeGB,disks[2].managedDisk.id
查询结果示例:
附录一:新创建的VM如果时托管类的Disk,则Disk Size信息无法直接在Type == ‘microsoft.compute/virtualmachines’ 中获取到,需要在 type=='microsoft.compute/disks' 资源中关联出来。
参考示例
Resources | where type == "microsoft.compute/virtualmachines" | extend os = properties.storageProfile.imageReference.offer | extend sku = properties.storageProfile.imageReference.sku | extend hostName = properties.osProfile.computerName | extend vmSize = properties.hardwareProfile.vmSize | extend osDiskName = properties.storageProfile.osDisk.name | extend osDiskSize = properties.storageProfile.osDisk.diskSizeGB | extend dataDisks = properties.storageProfile.dataDisks | extend managedDiskId_0= tolower(tostring(dataDisks[0].managedDisk.id)) | extend managedDiskId_1 = tolower(tostring(dataDisks[1].managedDisk.id)) | join kind=leftouter ( Resources | where type == 'microsoft.compute/disks' | project managedDiskId_0 = tolower(id),dataDisk_0_Name=name,dataDisk_0_SizeGB=properties.diskSizeGB) on managedDiskId_0 | join kind=leftouter ( Resources | where type == 'microsoft.compute/disks' | project managedDiskId_1 = tolower(id),dataDisk_1_Name=name,dataDisk_1_SizeGB=properties.diskSizeGB) on managedDiskId_1 | project subscriptionId, vmName = name, resourceGroup, location, hostName, os, sku, osDiskName, osDiskSize, vmSize, dataDisks, managedDiskId_0, dataDisks[0].name, dataDisk_0_diskSizeGB=iif(isnull(dataDisks[0].diskSizeGB),dataDisk_0_SizeGB,dataDisks[0].diskSizeGB), managedDiskId_1, dataDisks[1].name, dataDisk_1_diskSizeGB=iif(isnull(dataDisks[1].diskSizeGB),dataDisk_1_SizeGB,dataDisks[1].diskSizeGB), properties
参考文档:
Azure Virtual Machines : https://docs.microsoft.com/en-us/azure/governance/resource-graph/samples/samples-by-category?tabs=azure-cli#azure-virtual-machines
Query operators : https://docs.microsoft.com/en-us/azure/data-explorer/kusto/query/queries