azure - 导出互联网络 |子网

标签 azure powershell report

我想导出一些内容,例如 VNET 中存在的子网 View (如门户中显示的那样)。不幸的是,没有选项可以将其导出为 CSV。我在网上找到了可以导出子网路由表和关联子网的 powershell 脚本。我还找到了用于导出 vnet 子网详细信息的 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"
}

最佳答案

您收到的此错误清楚地表明变量 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"
}

关于azure - 导出互联网络 |子网,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72020042/

相关文章:

azure - 如何在 App Insights 中保存共享查询

azure - 使用 Azure Synapse Analytics 复制数据自动检测数据类型(导入)

powershell - Send-MailMessage : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Body' . 不支持指定的方法

javascript - 如何在 birt 报告中包装列数据

python - 如何获取scrapy失败的URL?

performance - 在 Web 应用程序中使用 .jasper 还是 .jrxml 更好?

azure - 为什么我应该使用托管身份?

c# - 更新 Azure 表存储中的 RowKey 或 PartitionKey

arrays - 是否可以在 PowerShell 中使 IndexOf 不区分大小写?

python - Anaconda Prompt 和 Anaconda Powershell Prompt 有什么区别?