从子网打印所有 VM 名称和私有 IP

问题描述

我想列出在特定子网(例如,名为“sub-a”)下包含私有 IP 地址的所有虚拟机名称。我该怎么做?

我希望 Azure Resource Graph Explorer 中的这个查询至少会打印出所有非空的私有 IP 地址:

Resources
| where type =~ 'microsoft.compute/virtualmachines' and isnotempty(properties.privateIPAddress)

解决方法

您需要查看网络接口并展开属性以提取私有 IP 地址。像这样的事情应该可以解决问题。我修改了我们的 examples 之一以提取私有 IP 而不是公共 IP。

 Resources
| where type =~ 'microsoft.compute/virtualmachines'
| extend nics=array_length(properties.networkProfile.networkInterfaces)
| mv-expand nic=properties.networkProfile.networkInterfaces
| where nics == 1 or nic.properties.primary =~ 'true' or isempty(nic)
| project vmId = id,vmName = name,vmSize=tostring(properties.hardwareProfile.vmSize),nicId = tostring(nic.id)
| join kind=leftouter (
    Resources
    | where type =~ 'microsoft.network/networkinterfaces'
    | extend ipConfigsCount=array_length(properties.ipConfigurations)
    | extend subnet = tostring(properties.ipConfigurations[0].properties.subnet) 
    | mv-expand ipconfig=properties.ipConfigurations
    | where ipConfigsCount == 1 or ipconfig.properties.primary =~ 'true'
    | project nicId = id,subnet,privateIp = tostring(ipconfig.properties.privateIPAddress))
on nicId
    | order by subnet asc