azure 导出Vnet|子网路

hrirmatl  于 2022-11-25  发布在  其他
关注(0)|答案(2)|浏览(166)

我想导出VNET中存在的子网视图,如门户中显示的视图。遗憾的是,没有将其导出为CSV的选项。我在网上找到了可以导出子网路由表和关联子网的powershell脚本。我还找到了可以导出有关vnets子网详细信息的powershell脚本。但是,我无法找到将两者结合起来的脚本
Script for Route tables by Aman Sharma
忽略概要和描述,我认为他离开他们从以前的脚本
因此我尝试反转逻辑,即获取子网详细信息,并添加每个子网的路由表(如果存在)。但是,我不确定我此时在做什么!脚本出错:

Line |
  47 |  …         $routeTables = Get-AzRouteTable -Name $routeTableName -Resour …
     |                                                  ~~~~~~~~~~~~~~~
     | Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.

脚本结束,CSV包含了除路由表细节之外的所有内容。因此,如果你能帮助一个新手,我将非常感激,这里是我所拥有的:

$PathToOutputCSVReport = "/path/output.csv"

$subs = Get-AzSubscription

#Checking if the subscriptions are found or not
if(($subs -ne $null) -or ($subs.Count -gt 0))
{
    #Creating Output Object
    $results = @()

    #Iterating over various subscriptions
    foreach($sub in $subs)
    {
        $SubscriptionId = $sub.SubscriptionId
        Write-Output $SubscriptionName

        #Selecting the Azure Subscription
        Select-AzSubscription -SubscriptionName $SubscriptionId

        #Getting all Azure Route Tables
        $vnets = Get-AzVirtualNetwork

        foreach($vnet in $vnets)
        {
            $vnetName = $vnet.Name
            $vnetResourceGroup = $vnet.ResourceGroupName
            Write-Output $vnetName

            #Fetch Route Subnets
            $vnetSubnets = $vnet.Subnets

            foreach($vnetSubnet in $vnetSubnets)
            {
                $subnetName = $vnetSubnet.Name
                Write-Output $subnetName

                $subnetId = $vnetSubnet.Id

                ###Getting information
                $splitarray = $subnetId.Split('/')
                $subscriptionId = $splitarray[2]
                $vNetResourceGroupName = $splitarray[4]
                $virtualNetworkName = $splitarray[8]
                $subnetName = $splitarray[10]

                #Fetch the route table details
                $routeTables = Get-AzRouteTable -Name $routeTableName -ResourceGroupName $routeResourceGroup

                #Fetching the vNet and Subnet details
                #$vnet = Get-AzVirtualNetwork -Name $virtualNetworkName -ResourceGroupName $vNetResourceGroupName
                $subnet = Get-AzVirtualNetworkSubnetConfig -Name $subnetName -VirtualNetwork $vnet
        
                $subnetAddressPrefix = $subnet.AddressPrefix[0]

                $details = @{            
                        virtualNetworkName=$virtualNetworkName
                        subnetAddressPrefix=$subnetAddressPrefix
                        subnetName=$subnetName
                        routeTableName=$routeTableName
                        routeResourceGroup=$routeResourceGroup
                        subscriptionId=$subscriptionId
                        vNetResourceGroupName=$vNetResourceGroupName
                        
                }                           
                $results += New-Object PSObject -Property $details
                

            }

        }
    }
    $results | export-csv -Path $PathToOutputCSVReport -NoTypeInformation
}
else
{
    Write-Host -ForegroundColor Red "No Subscription Found"
}
bksxznpy

bksxznpy1#

您收到的这个错误清楚地表明变量routeTableName对于cmdlet Get-AzRouteTable-Name参数是无效值。
查看您的脚本,似乎未在任何位置定义该变量,这就解释了为什么该变量为空,因此不能与cmdlet一起使用。
要使用与子网关联的路由表的名称定义变量,可以使用以下命令:

$routeTableName = $subnet.RouteTable.Id.Split('/')[8]

您似乎根本没有使用以下内容,可以将其删除:

$routeTables = Get-AzRouteTable -Name $routeTableName -ResourceGroupName $routeResourceGroup

整个代码如下所示:

$PathToOutputCSVReport = "/path/output.csv"

$subs = Get-AzSubscription

#Checking if the subscriptions are found or not
if(($subs -ne $null) -or ($subs.Count -gt 0))
{
    #Creating Output Object
    $results = @()

    #Iterating over various subscriptions
    foreach($sub in $subs)
    {
        $SubscriptionId = $sub.SubscriptionId
        Write-Output $SubscriptionName

        #Selecting the Azure Subscription
        Select-AzSubscription -SubscriptionName $SubscriptionId

        #Getting all Azure Route Tables
        $vnets = Get-AzVirtualNetwork

        foreach($vnet in $vnets)
        {
            $vnetName = $vnet.Name
            $vnetResourceGroup = $vnet.ResourceGroupName
            Write-Output $vnetName

            #Fetch Route Subnets
            $vnetSubnets = $vnet.Subnets

            foreach($vnetSubnet in $vnetSubnets)
            {
                $subnetName = $vnetSubnet.Name
                Write-Output $subnetName

                $subnetId = $vnetSubnet.Id

                ###Getting information
                $splitarray = $subnetId.Split('/')
                $subscriptionId = $splitarray[2]
                $vNetResourceGroupName = $splitarray[4]
                $virtualNetworkName = $splitarray[8]
                $subnetName = $splitarray[10]

                #Fetch the route table details
                #$routeTables = Get-AzRouteTable -Name $routeTableName -ResourceGroupName $routeResourceGroup

                #Fetching the vNet and Subnet details
                #$vnet = Get-AzVirtualNetwork -Name $virtualNetworkName -ResourceGroupName $vNetResourceGroupName
                $subnet = Get-AzVirtualNetworkSubnetConfig -Name $subnetName -VirtualNetwork $vnet
                $routeTableName = $subnet.RouteTable.Id.Split('/')[8]
        
                $subnetAddressPrefix = $subnet.AddressPrefix[0]

                $details = @{            
                        virtualNetworkName=$virtualNetworkName
                        subnetAddressPrefix=$subnetAddressPrefix
                        subnetName=$subnetName
                        routeTableName=$routeTableName
                        routeResourceGroup=$routeResourceGroup
                        subscriptionId=$subscriptionId
                        vNetResourceGroupName=$vNetResourceGroupName
                        
                }                           
                $results += New-Object PSObject -Property $details
                

            }

        }
    }
    $results | export-csv -Path $PathToOutputCSVReport -NoTypeInformation
}
else
{
    Write-Host -ForegroundColor Red "No Subscription Found"
}
ckocjqey

ckocjqey2#

我们也试过同样的方法,得到的警告和你们的一样,

查看您的脚本,您的操作是正确的,但没有提供任何变量来读取第47行的$routeTableName$routeResourceGroup。我们再次使用foreach循环来检索路由表详细信息,作为您使用的子网和vnet,然后执行以下脚本,我们可以无故障地运行它:-

$PathToOutputCSVReport = "/mylocalpath/output.csv"

$subs = Get-AzSubscription

#Checking if the subscriptions are found or not
if(($subs -ne $null) -or ($subs.Count -gt 0))
{
    #Creating Output Object
    $results = @()

    #Iterating over various subscriptions
    foreach($sub in $subs)
    {
        $SubscriptionId = $sub.SubscriptionId
        Write-Output $SubscriptionName

        #Selecting the Azure Subscription
        Select-AzSubscription -SubscriptionName $SubscriptionId

        #Getting all Azure Route Tables
        $vnets = Get-AzVirtualNetwork

        foreach($vnet in $vnets)
        {
            $vnetName = $vnet.Name
            $vnetResourceGroup = $vnet.ResourceGroupName
            Write-Output $vnetName

            #Fetch Route Subnets
            $vnetSubnets = $vnet.Subnets

            foreach($vnetSubnet in $vnetSubnets)
            {
                $subnetName = $vnetSubnet.Name
                Write-Output $subnetName

                $subnetId = $vnetSubnet.Id

                ###Getting information
                $splitarray = $subnetId.Split('/')
                $subscriptionId = $splitarray[2]
                $vNetResourceGroupName = $splitarray[4]
                $virtualNetworkName = $splitarray[8]
                $subnetName = $splitarray[10]

                #Fetch the route table details
                #$routeTables = Get-AzRouteTable -ResourceGroupName $routeResourceGroup -Name $routeTableName instead of this tried below line to fetch the route table details
                
        $routeTables = Get-AzRouteTable

        foreach($routeTable in $routeTables)
        {
            $routeTableName = $routeTable.Name
            $routeResourceGroup = $routeTable.ResourceGroupName
            Write-Output $routeName 

                #Fetching the vNet and Subnet details
                #$vnet = Get-AzVirtualNetwork -Name $virtualNetworkName -ResourceGroupName $vNetResourceGroupName
                $subnet = Get-AzVirtualNetworkSubnetConfig -Name $subnetName -VirtualNetwork $vnet
        
                $subnetAddressPrefix = $subnet.AddressPrefix[0]

                $details = @{            
                        virtualNetworkName=$virtualNetworkName
                        subnetAddressPrefix=$subnetAddressPrefix
                        subnetName=$subnetName
                        routeTableName=$routeTableName
                        routeResourceGroup=$routeResourceGroup
                        subscriptionId=$subscriptionId
                        vNetResourceGroupName=$vNetResourceGroupName
                        
                }                           
                $results += New-Object PSObject -Property $details
                
               }
            }

        }
    }
    $results | export-csv -Path $PathToOutputCSVReport -NoTypeInformation
}
else
{
    Write-Host -ForegroundColor Red "No Subscription Found"
}

您分享的链接也会根据您的要求列出。


输出详情供参考:-

CSV文件:-

相关问题