使用powershell获取Azure虚拟网络路由信息

问题描述 投票:0回答:1

我正在尝试生成一份显示天蓝色虚拟网络路由信息的报告。我有一个获取此信息的脚本,但是检索信息的方式需要改进。

层次结构如下,

vnet >> 子网 >> 路由表 >> 路由

一个路由表可以定义多条路由。下面脚本的问题在于路由以逗号列表连接在一起。这使得阅读报告变得困难,我想做的是让报告为每个路由显示一行,因为路由表可以有多个路由,所以对于定义的每个路由,其余的其他信息保持不变是

SubscriptionName,ResourceGroupName,VNetName,AddressSpace,SubnetName

这就是我目前所拥有的。

我正在努力实现这个目标

对于

RoutedCorrectly
列,规则是如果
NextHopIpAddress
10.14.12.13
10.17.12.13
,则如果
NextHopIpAddress
为任何其他值,则该字段应报告 Y,然后
RoutedCorrectly
设置为 N。

到目前为止的代码。

$report = @()

$subs = Get-AzSubscription
$file_date_suffix = $((Get-Date).ToString("ddMMyyyy_HHmmss"))
$path = "C:\files\report_$file_date_suffix.csv"

foreach ($Sub in $Subs) {
    Select-AzSubscription -SubscriptionName $Sub.Name | Out-Null
    
    $vnets = Get-AzVirtualNetwork
    foreach ($vnet in $vnets) {
        foreach ($subnet in $vnet.Subnets) {
            $vnetInfo = [PSCustomObject]@{
                SubscriptionName    = $Sub.Name
                ResourceGroupName   = $vnet.ResourceGroupName
                VNetName            = $vnet.Name
                AddressSpace        = $vnet.AddressSpace.AddressPrefixes -join ','
                SubnetName          = $subnet.Name
                RouteTable          = ""
                RouteName           = ""
                AddressPrefix       = ""
                NextHopType  =        ""
                NextHopIpAddress    = ""
                RoutedCorrectly     = ""
            }

            $subnet_config = Get-AzVirtualNetworkSubnetConfig -Name $subnet.Name -VirtualNetwork $vnet
            # $subnet_config | Format-List
            $RouteTableJson = $subnet_config.RouteTableText | ConvertFrom-Json
            $rt_rg = [regex]::Match($RouteTableJson.ID, '/resourceGroups/([^/]+)/').Groups[1].Value


            if ($rt_rg) #only if a route is defined
            {
            $routetables = Get-AzRouteTable -ResourceGroupName $rt_rg
            foreach ($routetable in $routetables) {
                $vnetInfo.RouteTable += $routetable.Name + ","

            }

            $routeConfigurations = Get-AzRouteTable -ResourceGroupName $rt_rg | Get-AzRouteConfig | Select-Object Name, NextHopType, NextHopIpAddress, AddressPrefix
            foreach ($routeConfiguration in $routeConfigurations)
            {
                $vnetInfo.RouteName += $routeConfiguration.Name + ","
                $vnetInfo.AddressPrefix += $routeConfiguration.AddressPrefix + ","
                $vnetInfo.NextHopType += $routeConfiguration.NextHopType + ","
                $vnetInfo.NextHopIpAddress += $routeConfiguration.NextHopIpAddress + ","
                $vnetInfo.RoutedCorrectly +=  ""


            }

        }

        else{

            $vnetInfo.RouteTable += "Undefined"

        }

        $vnetInfo.RouteTable = $vnetInfo.RouteTable.TrimEnd(',')

            $report += $vnetInfo
        }
    }
}

$report | Export-Csv -Path $path -NoTypeInformation -Encoding ASCII
azure powershell azure-virtual-network
1个回答
0
投票

我尝试使用 powershell 获取 Azure 虚拟网络路由信息,并且能够成功配置要求。

脚本工作正常,但路由之间用逗号分隔,这降低了报告的清晰度。为了使其更好,我们应该将每条路由放在不同的线路上,并包含每条线路的 VNet、子网和路由表的详细信息。

我更新的powershell脚本:

# Gather report data
$report = @()
$subs = Get-AzSubscription
$file_date_suffix = $((Get-Date).ToString("ddMMyyyy_HHmmss"))
$path = "C:\Users\demo.csv"

foreach ($Sub in $Subs) {
    Select-AzSubscription -SubscriptionName $Sub.Name | Out-Null
    
    $vnets = Get-AzVirtualNetwork
    foreach ($vnet in $vnets) {
        foreach ($subnet in $vnet.Subnets) {
            $subnet_config = Get-AzVirtualNetworkSubnetConfig -Name $subnet.Name -VirtualNetwork $vnet
            $RouteTableJson = $subnet_config.RouteTableText | ConvertFrom-Json
            $rt_rg = [regex]::Match($RouteTableJson.ID, '/resourceGroups/([^/]+)/').Groups[1].Value

            if ($rt_rg) {
                $routetables = Get-AzRouteTable -ResourceGroupName $rt_rg
                foreach ($routetable in $routetables) {
                    $routeConfigurations = $routetable | Get-AzRouteConfig
                    foreach ($routeConfiguration in $routeConfigurations) {
                        $routedCorrectly = if ($routeConfiguration.NextHopIpAddress -eq "10.14.12.13" -or $routeConfiguration.NextHopIpAddress -eq "10.17.12.13") {"Y"} else {"N"}
                        $report += [PSCustomObject]@{
                            SubscriptionName    = $Sub.Name
                            ResourceGroupName   = $vnet.ResourceGroupName
                            VNetName            = $vnet.Name
                            AddressSpace        = $vnet.AddressSpace.AddressPrefixes -join ','
                            SubnetName          = $subnet.Name
                            RouteTable          = $routetable.Name
                            RouteName           = $routeConfiguration.Name
                            AddressPrefix       = $routeConfiguration.AddressPrefix
                            NextHopType         = $routeConfiguration.NextHopType
                            NextHopIpAddress    = $routeConfiguration.NextHopIpAddress
                            RoutedCorrectly     = $routedCorrectly
                        }
                    }
                }
            } else {
                $report += [PSCustomObject]@{
                    SubscriptionName    = $Sub.Name
                    ResourceGroupName   = $vnet.ResourceGroupName
                    VNetName            = $vnet.Name
                    AddressSpace        = $vnet.AddressSpace.AddressPrefixes -join ','
                    SubnetName          = $subnet.Name
                    RouteTable          = "Undefined"
                    RouteName           = ""
                    AddressPrefix       = ""
                    NextHopType         = ""
                    NextHopIpAddress    = ""
                    RoutedCorrectly     = ""
                }
            }
        }
    }
}

# Export report to CSV
$report | Export-Csv -Path $path -NoTypeInformation -Encoding ASCII

此脚本应按照您要求的格式生成一份报告,每行一条路线

输出:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.