获取当前 Azure 租户下的所有虚拟网络(VNet)

2026-07-15#Azure#VNet

在 Azure 创建虚拟网络(VNet)时,通常需要规划 IP 地址空间,以避免与其他 VNet 或本地网络冲突。为了更好地规划和管理 VNet,了解当前 Azure 租户下的所有 VNet 及其地址空间是非常重要的。为此,可使用 Azure Resource Graph 查询当前租户下的所有 VNet 信息。

查询语句如下所示,包括订阅名称、资源组、VNet 名称、地址空间和地区等:

resources
| where type =~ 'microsoft.network/virtualnetworks'
// 1. 如果一个VNet有多个大网段,展开成多行,方便排序
| mv-expand addressPrefix = properties.addressSpace.addressPrefixes
// 2. 关联订阅表获取订阅名称
| join kind=leftouter (
    resourcecontainers
    | where type =~ 'microsoft.resources/subscriptions'
    | project SubscriptionName = name, RightSubId = subscriptionId
) on $left.subscriptionId == $right.RightSubId
// 3. 只投影VNet级别的核心规划字段
| project
    SubscriptionName,
    SubscriptionId = subscriptionId,
    ResourceGroup = resourceGroup,
    VNetName = name,
    VNetAddressSpace = tostring(addressPrefix),
    Location = location
// 4. 按网段排序,空闲段一目了然
| order by VNetAddressSpace asc