获取扩展属性与其他属性

huangapple go评论103阅读模式
英文:

Getting extended properties with other properties

问题

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

获取扩展属性与其他属性

$gatewayDetailsArray = @()

# 循环遍历每个订阅
#foreach ($subscription in $subscriptions) {
 #   Write-Host "Processing Subscription: $($subscription.Name)" -ForegroundColor Yellow

    # 选择当前订阅
  #   Select-AzSubscription -SubscriptionId $subscription.Id

    # 获取当前订阅中的所有资源组
    $resourceGroups = Get-AzResourceGroup

    # 循环遍历每个资源组以获取虚拟网络网关
    foreach ($resourceGroup in $resourceGroups) {
       # $gateways = Get-AzVirtualNetworkGateway -ResourceGroupName $resourceGroup.ResourceGroupName  | Select-Object -Property Name, GatewayType, VpnType, Sku -ExpandProperty IpConfigurations

        
       # 循环遍历每个虚拟网络网关,并将详细信息添加到数组中
        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
        }
    }
#}

# 导出网关详细信息数组到 CSV 文件
$csvFilePath2 = "C:\temp\GatewayDetails_2.csv"
$gatewayDetailsArray | Export-Csv -Path $csvFilePath2 -NoTypeInformation
英文:

I have a PS script that collects information for all Az Virtual Network gateways but there seems to be an issue getting name of the resource. I am getting Name with red arrow in a screenshot instead of Name with Yellow arrow.

获取扩展属性与其他属性

$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


</details>


# 答案1
**得分**: 1

以下是修改后的脚本的翻译部分:

```c
# $subscriptions = Get-AzSubscription

# 初始化一个数组以存储网关详细信息
$gatewayDetailsArray = @()

# 遍历每个订阅
#foreach ($subscription in $subscriptions) {
 #   Write-Host &quot;正在处理订阅:$($subscription.Name)&quot; -ForegroundColor Yellow

    # 选择当前订阅
  #   Select-AzSubscription -SubscriptionId $subscription.Id

    # &lt;span class=&quot; active-doc-0&quot; data-doc-items=&quot;0&quot;&gt;获取当前订阅中的所有资源组[1](#doc-pos=0)&lt;/span&gt;
    $resourceGroups = Get-AzResourceGroup

    # 遍历每个资源组以获取虚拟网络网关
    foreach ($resourceGroup in $resourceGroups) {
        $gateways = Get-AzVirtualNetworkGateway -ResourceGroupName $resourceGroup.ResourceGroupName  | Select-Object -ExpandProperty Name

        
       # &lt;span class=&quot; active-doc-1&quot; data-doc-items=&quot;1&quot;&gt;遍历每个虚拟网络网关并将详细信息添加到数组中[2](#doc-pos=1)&lt;/span&gt;
        foreach ($gateway in $gateways) {
            $details = @{
                订阅 = $subscription.Name
                资源组名称 = $resourceGroup.ResourceGroupName
                名称 = $gateway
            }
            $gatewayDetailsArray += New-Object PSObject -Property $details
        }
    }
#}

# 输出网关详细信息
$gatewayDetailsArray | Format-Table -AutoSize

# 将网关详细信息数组导出到CSV文件
$csvFilePath2 = &quot;C:\temp\GatewayDetails_2.csv&quot;
$gatewayDetailsArray | Export-Csv -Path $csvFilePath2 -NoTypeInformation

输出

获取扩展属性与其他属性

在csv文件中,导出的内容如下:

获取扩展属性与其他属性

更新

> 我们希望输出如下。网关名称,订阅ID,资源组,VPN类型,SKU名称,来自IP配置的子网ID(参考原始截图),以便我们可以提取VNET详细信息

您可以使用以下命令获取所有详细信息如下:

# 初始化一个数组以存储网关详细信息
$gatewayDetailsArray = @()

# 获取所有订阅
$subscriptions = Get-AzSubscription

# 遍历每个订阅
foreach ($subscription in $subscriptions) {
    Write-Host &quot;正在处理订阅:$($subscription.Name)&quot; -ForegroundColor Yellow

    # 选择当前订阅
    Select-AzSubscription -SubscriptionId $subscription.Id

    # 获取当前订阅中的所有资源组
    $resourceGroups = Get-AzResourceGroup

    # 遍历每个资源组以获取虚拟网络网关
    foreach ($resourceGroup in $resourceGroups) {
        $gateways = Get-AzVirtualNetworkGateway -ResourceGroupName $resourceGroup.ResourceGroupName | Select-Object -Property Name, @{Name=&quot;订阅ID&quot;;Expression={$subscription.Id}}, ResourceGroupName, VpnType, @{Name=&quot;SKU名称&quot;;Expression={$_.Sku.Name}}, @{Name=&quot;子网ID&quot;;Expression={$_.IpConfigurations.Subnet.Id}}, @{Name=&quot;子网名称&quot;;Expression={(Get-AzVirtualNetworkSubnetConfig -ResourceId $_.IpConfigurations.Subnet.Id).Name}}

        # 遍历每个虚拟网络网关并将详细信息添加到数组中
        foreach ($gateway in $gateways) {
            $gatewayDetailsArray += $gateway
        }
    }
}

# 输出网关详细信息
$gatewayDetailsArray | Format-Table -AutoSize 名称, 订阅ID, 资源组名称, VPN类型, SKU名称, 子网ID, 子网名称

# 将网关详细信息数组导出到CSV文件
$csvFilePath2 = &quot;C:\temp\GatewayDetails_2.csv&quot;
$gatewayDetailsArray | Export-Csv -Path $csvFilePath2 -NoTypeInformation

获取扩展属性与其他属性

获取扩展属性与其他属性

英文:

To collect information of all Virtual Network gateways in your subscription, you can make use of the below modified script:

# $subscriptions = Get-AzSubscription

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

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

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

    # &lt;span class=&quot; active-doc-0&quot; data-doc-items=&quot;0&quot;&gt;Get all resource groups in the current subscription[1](#doc-pos=0)&lt;/span&gt;
    $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

        
       # &lt;span class=&quot; active-doc-1&quot; data-doc-items=&quot;1&quot;&gt;Loop through each virtual network gateway and add details to the array[2](#doc-pos=1)&lt;/span&gt;
        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 = &quot;C:\temp\GatewayDetails_2.csv&quot;
$gatewayDetailsArray | Export-Csv -Path $csvFilePath2 -NoTypeInformation

Output:

获取扩展属性与其他属性

In csv, file got exported like below:

获取扩展属性与其他属性


# $subscriptions = Get-AzSubscription

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

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

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

    # &lt;span class=&quot; active-doc-0&quot; data-doc-items=&quot;0&quot;&gt;Get all resource groups in the current subscription[1](#doc-pos=0)&lt;/span&gt;
    $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

        
       # &lt;span class=&quot; active-doc-1&quot; data-doc-items=&quot;1&quot;&gt;Loop through each virtual network gateway and add details to the array[2](#doc-pos=1)&lt;/span&gt;
        foreach ($gateway in $gateways) {
            $details = @{
                Subscription = $subscription.Name
                ResourceGroupName = $resourceGroup.ResourceGroupName
                Name = $gateway
                vpnType = $gateway.VpnType
                GatewayType = $gateway.GatewayType
                SKU = $gateway.Sku.Name
                VNET = ($gateway.subnet.id -split &#39;/&#39;)[-3]
            }
            $gatewayDetailsArray += New-Object PSObject -Property $details
        }
    }
#}

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


$csvFilePath2 = &quot;C:\temp\GatewayDetails_2.csv&quot;
$gatewayDetailsArray | Export-Csv -Path $csvFilePath2 -NoTypeInformation

Update

> We would like below in output. Gatway name, Subscription ID, Resource group, VPNType, SKU name, Subnet ID from IPconfigurations (refer to original screenshot) so that we can extract VNET detail

You can make use of below command to get all the details like below:

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

# Get all subscriptions
$subscriptions = Get-AzSubscription

# Loop through each subscription
foreach ($subscription in $subscriptions) {
    Write-Host &quot;Processing Subscription: $($subscription.Name)&quot; -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, @{Name=&quot;SubscriptionID&quot;;Expression={$subscription.Id}}, ResourceGroupName, VpnType, @{Name=&quot;SKUName&quot;;Expression={$_.Sku.Name}}, @{Name=&quot;SubnetID&quot;;Expression={$_.IpConfigurations.Subnet.Id}}, @{Name=&quot;SubnetName&quot;;Expression={(Get-AzVirtualNetworkSubnetConfig -ResourceId $_.IpConfigurations.Subnet.Id).Name}}

        # Loop through each virtual network gateway and add details to the array
        foreach ($gateway in $gateways) {
            $gatewayDetailsArray += $gateway
        }
    }
}

# Output the gateway details
$gatewayDetailsArray | Format-Table -AutoSize Name, SubscriptionID, ResourceGroupName, VpnType, SKUName, SubnetID, SubnetName

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

获取扩展属性与其他属性

获取扩展属性与其他属性

huangapple
  • 本文由 发表于 2023年8月9日 10:30:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76864212-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定