出口Vnet |子网

Export Vnet | Subnet

我想导出一些内容,例如门户中显示的 VNET 中存在的子网视图。不幸的是,没有将其导出为 CSV 的选项。我在网上找到了可以导出子网路由 tables 和相关子网的 powershell 脚本。我还找到了 powershell 脚本来导出有关 vnets 子网的详细信息。但是我一直没能找到结合两者的脚本

Script for Route tables by Aman Sharma

忽略大纲和描述,我认为他在之前的剧本中留下了它们

所以我试图反转逻辑,即获取子网详细信息并为每个子网添加路由 tables(如果存在)。但是我不确定此时我在做什么!脚本错误:

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 包含除路线 table 详细信息之外的所有内容。所以,如果你能帮助一个菜鸟,我将非常感激,这就是我所拥有的:

$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"
}

您遇到的这个错误清楚地表明变量 routeTableName 是 cmdlet Get-AzRouteTable.

-Name 参数的无效值

查看您的脚本,该变量似乎未在任何地方定义,这解释了为什么它为空,因此无法与 cmdlet 一起使用。

要使用关联到子网的路由名称 table 定义变量,您可以使用以下内容:

$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"
}

我们已经尝试过和你一样的警告,

看看你的脚本,你做的是正确的,但没有提供任何变量来读取第 47 行的 $routeTableName$routeResourceGroup。我们再次使用 foreach 循环来检索路由 table 详细信息,如您使用的子网和 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"
}

此外,您分享的link也会根据您的要求列出。

输出详细信息以供参考:-

CSV 文件:-

有关以不同方式获取路线详细信息的更多信息,请参阅以下 links:-

  • ,