使用其他属性获取扩展属性

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

我有一个 PS 脚本,用于收集所有 Az 虚拟网络网关的信息,但获取资源名称似乎存在问题。我在屏幕截图中看到带有红色箭头的名称,而不是带有黄色箭头的名称。

# $subscriptions = Get-AzSubscription

# Initialize an array to store gateway details
$gatewayDetailsArray = @()

# Loop through each subscription
#foreach ($subscription in $subscriptions) {
 #   Write-Host "Processing Subscription: $($subscription.Name)" -ForegroundColor Yellow

    # Select the current subscription
  #   Select-AzSubscription -SubscriptionId $subscription.Id

    # Get all resource groups in the current subscription
    $resourceGroups = Get-AzResourceGroup

    # Loop through each resource group to get virtual network gateways
    foreach ($resourceGroup in $resourceGroups) {
       # $gateways = Get-AzVirtualNetworkGateway -ResourceGroupName $resourceGroup.ResourceGroupName  | Select-Object -Property Name, GatewayType, VpnType, Sku -ExpandProperty IpConfigurations

        
       # Loop through each virtual network gateway and add details to the array
        foreach ($gateway in $gateways) {
            $details = @{
                Subscription = $subscription.Name
                ResourceGroupName = $gateway.ResourceGroupName
                Name = $gateway.Name
                GatewayType = $gateway.GatewayType
                VpnType = $gateway.VpnType
                GatewayIP = $gateway.GatewayIpAddress
                SKU = $gateway.Sku.Name
                VNET = ($gateway.subnet.id -split '/')[-3]
            }
            $gatewayDetailsArray += New-Object PSObject -Property $details
        }
    }
#}

# Export the gateway details array to a CSV file
$csvFilePath2 = "C:\temp\GatewayDetails_2.csv"
$gatewayDetailsArray | Export-Csv -Path $csvFilePath2 -NoTypeInformation
azure powershell azure-virtual-network
1个回答
0
投票

要收集订阅中所有虚拟网关的信息,您可以使用以下修改后的脚本:

# $subscriptions = Get-AzSubscription

# Initialize an array to store gateway details
$gatewayDetailsArray = @()

# Loop through each subscription
#foreach ($subscription in $subscriptions) {
 #   Write-Host "Processing Subscription: $($subscription.Name)" -ForegroundColor Yellow

    # Select the current subscription
  #   Select-AzSubscription -SubscriptionId $subscription.Id

    # <span class=" active-doc-0" data-doc-items="0">Get all resource groups in the current subscription[1](#doc-pos=0)</span>
    $resourceGroups = Get-AzResourceGroup

    # Loop through each resource group to get virtual network gateways
    foreach ($resourceGroup in $resourceGroups) {
        $gateways = Get-AzVirtualNetworkGateway -ResourceGroupName $resourceGroup.ResourceGroupName  | Select-Object -ExpandProperty Name

        
       # <span class=" active-doc-1" data-doc-items="1">Loop through each virtual network gateway and add details to the array[2](#doc-pos=1)</span>
        foreach ($gateway in $gateways) {
            $details = @{
                Subscription = $subscription.Name
                ResourceGroupName = $resourceGroup.ResourceGroupName
                Name = $gateway
            }
            $gatewayDetailsArray += New-Object PSObject -Property $details
        }
    }
#}

# Output the gateway details
$gatewayDetailsArray | Format-Table -AutoSize

# Export the gateway details array to a CSV file
$csvFilePath2 = "C:\temp\GatewayDetails_2.csv"
$gatewayDetailsArray | Export-Csv -Path $csvFilePath2 -NoTypeInformation

输出

enter image description here

在 csv 中,文件导出如下:

enter image description here

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