如何使用 powershell 将 Azure 虚拟网络子网输出到 csv 以进行单个或多个订阅

How to output Azure Virtual Network Subnets to csv using powershell for single or multiple subscriptions

我是 powershell 的新手,正在尝试在单个或多个订阅中输出 Azure 虚拟网络的子网信息。

我可以输出到控制台并获得我正在寻找的结果,但很难将相同的输出输出到 csv 文件。

我知道我需要创建新对象等,但无法使用正确的语法。

有谁能帮忙吗

下面的工作代码....

$subs = Get-AzSubscription -SubscriptionID xxxxxxxxxxxxxxxx
foreach ($Sub in $Subs) { 
    $SelectSub = Select-AzSubscription -SubscriptionName $Sub.Name 
    $VNETs = Get-AzVirtualNetwork 
    foreach ($VNET in $VNETs) { 
        $Sub.Name 
        $VNET.Name 
        ($VNET).AddressSpace.AddressPrefixes 
        ($VNET).Subnets.Name
    Write-Host " "
    } 
} 

我认为像这样的东西可以解决问题:

$subs = Get-AzSubscription -SubscriptionID xxxxxxxxxxxxxxxx
foreach ($Sub in $Subs) { 
    Select-AzSubscription -SubscriptionName $Sub.Name | Out-Null
    $networks = Get-AzVirtualNetwork | ForEach-Object {
        New-Object PSObject -Property @{
            name         = $_.name
            subnets      = $_.subnets.name -join ';'
            addressSpace = $_.AddressSpace.AddressPrefixes -join ';'
        }
    }
    $networks | Export-Csv -NoTypeInformation file.csv
}

编辑:以前的版本并没有真正起作用,我猜 export-csv 有点奇怪。