You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用PowerShell获取Azure的VNET、子网、NSG及路由表信息?

包含NSG和路由表信息的VNet子网设备清单脚本

以下是修改后的PowerShell脚本,已添加子网关联的网络安全组(NSG)和路由表信息:

$subs = Get-AzSubscription 
foreach ($Sub in $Subs) {
    Write-Host "***************************"
    Write-Host " "
    $Sub.Name 

    $SelectSub = Select-AzSubscription -SubscriptionName $Sub.Name

    $VNETs = Get-AzVirtualNetwork 
    foreach ($VNET in $VNETs) {
        Write-Host "--------------------------"
        Write-Host " "
        Write-Host "   vNet: " $VNET.Name 
        Write-Host "   AddressPrefixes: " ($VNET).AddressSpace.AddressPrefixes

        $vNetExpanded = Get-AzVirtualNetwork -Name $VNET.Name -ResourceGroupName $VNET.ResourceGroupName -ExpandResource 'subnets/ipConfigurations' 

        foreach($subnet in $vNetExpanded.Subnets)
        {
            Write-Host "       Subnet: " $subnet.Name
            # 提取关联NSG信息
            $nsgName = if ($subnet.NetworkSecurityGroup) { $subnet.NetworkSecurityGroup.Name } else { "未关联NSG" }
            Write-Host "          关联NSG: " $nsgName
            # 提取关联路由表信息
            $routeTableName = if ($subnet.RouteTable) { $subnet.RouteTable.Name } else { "未关联路由表" }
            Write-Host "          关联路由表: " $routeTableName
            Write-Host "          已连接设备数: " $subnet.IpConfigurations.Count
            foreach($ipConfig in $subnet.IpConfigurations)
            {
                Write-Host "            " $ipConfig.PrivateIpAddress
            }
        }

        Write-Host " " 
    } 
}

修改说明

  • 在子网循环逻辑中,通过$subnet.NetworkSecurityGroup和$subnet.RouteTable直接读取子网关联的资源对象,判断资源是否存在后输出对应名称,无关联资源时显示提示文本
  • 优化了设备数的显示描述,让输出内容更易读

内容的提问来源于stack exchange,提问作者Blaster Away

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 12:15:33