获取其他属性的扩展属性

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

Getting extended properties with other properties

问题

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

获取其他属性的扩展属性

  1. $gatewayDetailsArray = @()
  2. # 循环遍历每个订阅
  3. #foreach ($subscription in $subscriptions) {
  4. # Write-Host "Processing Subscription: $($subscription.Name)" -ForegroundColor Yellow
  5. # 选择当前订阅
  6. # Select-AzSubscription -SubscriptionId $subscription.Id
  7. # 获取当前订阅中的所有资源组
  8. $resourceGroups = Get-AzResourceGroup
  9. # 循环遍历每个资源组以获取虚拟网络网关
  10. foreach ($resourceGroup in $resourceGroups) {
  11. # $gateways = Get-AzVirtualNetworkGateway -ResourceGroupName $resourceGroup.ResourceGroupName | Select-Object -Property Name, GatewayType, VpnType, Sku -ExpandProperty IpConfigurations
  12. # 循环遍历每个虚拟网络网关并将详细信息添加到数组中
  13. foreach ($gateway in $gateways) {
  14. $details = @{
  15. Subscription = $subscription.Name
  16. ResourceGroupName = $gateway.ResourceGroupName
  17. Name = $gateway.Name
  18. GatewayType = $gateway.GatewayType
  19. VpnType = $gateway.VpnType
  20. GatewayIP = $gateway.GatewayIpAddress
  21. SKU = $gateway.Sku.Name
  22. VNET = ($gateway.subnet.id -split '/')[3]
  23. }
  24. $gatewayDetailsArray += New-Object PSObject -Property $details
  25. }
  26. }
  27. #}
  28. # 将网关详细信息数组导出为 CSV 文件
  29. $csvFilePath2 = "C:\temp\GatewayDetails_2.csv"
  30. $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.

获取其他属性的扩展属性

  1. $gatewayDetailsArray = @()
  2. # Loop through each subscription
  3. #foreach ($subscription in $subscriptions) {
  4. # Write-Host "Processing Subscription: $($subscription.Name)" -ForegroundColor Yellow
  5. # Select the current subscription
  6. # Select-AzSubscription -SubscriptionId $subscription.Id
  7. # Get all resource groups in the current subscription
  8. $resourceGroups = Get-AzResourceGroup
  9. # Loop through each resource group to get virtual network gateways
  10. foreach ($resourceGroup in $resourceGroups) {
  11. # $gateways = Get-AzVirtualNetworkGateway -ResourceGroupName $resourceGroup.ResourceGroupName | Select-Object -Property Name, GatewayType, VpnType, Sku -ExpandProperty IpConfigurations
  12. # Loop through each virtual network gateway and add details to the array
  13. foreach ($gateway in $gateways) {
  14. $details = @{
  15. Subscription = $subscription.Name
  16. ResourceGroupName = $gateway.ResourceGroupName
  17. Name = $gateway.Name
  18. GatewayType = $gateway.GatewayType
  19. VpnType = $gateway.VpnType
  20. GatewayIP = $gateway.GatewayIpAddress
  21. SKU = $gateway.Sku.Name
  22. VNET = ($gateway.subnet.id -split '/')[-3]
  23. }
  24. $gatewayDetailsArray += New-Object PSObject -Property $details
  25. }
  26. }
  27. #}
  28. # Export the gateway details array to a CSV file
  29. $csvFilePath2 = "C:\temp\GatewayDetails_2.csv"
  30. $gatewayDetailsArray | Export-Csv -Path $csvFilePath2 -NoTypeInformation
  31. </details>
  32. # 答案1
  33. **得分**: 1
  34. 要收集订阅中所有虚拟网络网关的信息,您可以使用以下修改后的脚本:
  35. ```c
  36. # $subscriptions = Get-AzSubscription
  37. # 初始化一个数组来存储网关详细信息
  38. $gatewayDetailsArray = @()
  39. # 循环遍历每个订阅
  40. #foreach ($subscription in $subscriptions) {
  41. # Write-Host &quot;Processing Subscription: $($subscription.Name)&quot; -ForegroundColor Yellow
  42. # 选择当前订阅
  43. # Select-AzSubscription -SubscriptionId $subscription.Id
  44. # &lt;span class=&quot; active-doc-0&quot; data-doc-items=&quot;0&quot;&gt;获取当前订阅中的所有资源组[1](#doc-pos=0)&lt;/span&gt;
  45. $resourceGroups = Get-AzResourceGroup
  46. # 循环遍历每个资源组以获取虚拟网络网关
  47. foreach ($resourceGroup in $resourceGroups) {
  48. $gateways = Get-AzVirtualNetworkGateway -ResourceGroupName $resourceGroup.ResourceGroupName | Select-Object -ExpandProperty Name
  49. # &lt;span class=&quot; active-doc-1&quot; data-doc-items=&quot;1&quot;&gt;循环遍历每个虚拟网络网关并将详细信息添加到数组中[2](#doc-pos=1)&lt;/span&gt;
  50. foreach ($gateway in $gateways) {
  51. $details = @{
  52. Subscription = $subscription.Name
  53. ResourceGroupName = $resourceGroup.ResourceGroupName
  54. Name = $gateway
  55. }
  56. $gatewayDetailsArray += New-Object PSObject -Property $details
  57. }
  58. }
  59. #}
  60. # 输出网关详细信息
  61. $gatewayDetailsArray | Format-Table -AutoSize
  62. # 将网关详细信息数组导出到CSV文件
  63. $csvFilePath2 = &quot;C:\temp\GatewayDetails_2.csv&quot;
  64. $gatewayDetailsArray | Export-Csv -Path $csvFilePath2 -NoTypeInformation

输出

获取其他属性的扩展属性

CSV文件导出的内容如下:

获取其他属性的扩展属性


  1. # $subscriptions = Get-AzSubscription
  2. # 初始化一个数组来存储网关详细信息
  3. $gatewayDetailsArray = @()
  4. # 循环遍历每个订阅
  5. #foreach ($subscription in $subscriptions) {
  6. # Write-Host &quot;Processing Subscription: $($subscription.Name)&quot; -ForegroundColor Yellow
  7. # 选择当前订阅
  8. # Select-AzSubscription -SubscriptionId $subscription.Id
  9. # &lt;span class=&quot; active-doc-0&quot; data-doc-items=&quot;0&quot;&gt;获取当前订阅中的所有资源组[1](#doc-pos=0)&lt;/span&gt;
  10. $resourceGroups = Get-AzResourceGroup
  11. # 循环遍历每个资源组以获取虚拟网络网关
  12. foreach ($resourceGroup in $resourceGroups) {
  13. $gateways = Get-AzVirtualNetworkGateway -ResourceGroupName $resourceGroup.ResourceGroupName | Select-Object -ExpandProperty Name
  14. # &lt;span class=&quot; active-doc-1&quot; data-doc-items=&quot;1&quot;&gt;循环遍历每个虚拟网络网关并将详细信息添加到数组中[2](#doc-pos=1)&lt;/span&gt;
  15. foreach ($gateway in $gateways) {
  16. $details = @{
  17. Subscription = $subscription.Name
  18. ResourceGroupName = $resourceGroup.ResourceGroupName
  19. Name = $gateway
  20. vpnType = $gateway.VpnType
  21. GatewayType = $gateway.GatewayType
  22. SKU = $gateway.Sku.Name
  23. VNET = ($gateway.subnet.id -split &#39;/&#39;)[-3]
  24. }
  25. $gatewayDetailsArray += New-Object PSObject -Property $details
  26. }
  27. }
  28. #}
  29. # 输出网关详细信息
  30. $gatewayDetailsArray | Format-Table -AutoSize
  31. $csvFilePath2 = &quot;C:\temp\GatewayDetails_2.csv&quot;
  32. $gatewayDetailsArray | Export-Csv -Path $csvFilePath2 -NoTypeInformation

更新

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

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

  1. # 初始化一个数组来存储网关详细信息
  2. $gatewayDetailsArray = @()
  3. # 获取所有订阅
  4. $subscriptions = Get-AzSubscription
  5. # 循环遍历每个订阅
  6. foreach ($subscription in $subscriptions) {
  7. Write-Host &quot;Processing Subscription: $($subscription.Name)&quot; -ForegroundColor Yellow
  8. # 选择当前订阅
  9. Select-AzSubscription -SubscriptionId $subscription.Id
  10. # 获取当前订阅中的所有资源组
  11. $resourceGroups = Get-AzResourceGroup
  12. # 循环遍历每个资源组以获取虚拟网络网关
  13. foreach ($resourceGroup in $resourceGroups) {
  14. $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}}
  15. # 循环遍历每个虚拟网络网关并将详细信息添加到数组中
  16. foreach ($gateway in $gateways) {
  17. $gatewayDetailsArray += $gateway
  18. }
  19. }
  20. }
  21. # 输出网关详细信息
  22. $gatewayDetailsArray | Format-Table -AutoSize Name, SubscriptionID, ResourceGroupName, VpnType, SKUName, SubnetID, SubnetName
  23. # 将网关详细信息数组导出到CSV文件
  24. $csvFilePath2 = &quot;C:\temp\GatewayDetails_2.csv&quot;
  25. $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:

  1. # $subscriptions = Get-AzSubscription
  2. # Initialize an array to store gateway details
  3. $gatewayDetailsArray = @()
  4. # Loop through each subscription
  5. #foreach ($subscription in $subscriptions) {
  6. # Write-Host &quot;Processing Subscription: $($subscription.Name)&quot; -ForegroundColor Yellow
  7. # Select the current subscription
  8. # Select-AzSubscription -SubscriptionId $subscription.Id
  9. # &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;
  10. $resourceGroups = Get-AzResourceGroup
  11. # Loop through each resource group to get virtual network gateways
  12. foreach ($resourceGroup in $resourceGroups) {
  13. $gateways = Get-AzVirtualNetworkGateway -ResourceGroupName $resourceGroup.ResourceGroupName | Select-Object -ExpandProperty Name
  14. # &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;
  15. foreach ($gateway in $gateways) {
  16. $details = @{
  17. Subscription = $subscription.Name
  18. ResourceGroupName = $resourceGroup.ResourceGroupName
  19. Name = $gateway
  20. }
  21. $gatewayDetailsArray += New-Object PSObject -Property $details
  22. }
  23. }
  24. #}
  25. # Output the gateway details
  26. $gatewayDetailsArray | Format-Table -AutoSize
  27. # Export the gateway details array to a CSV file
  28. $csvFilePath2 = &quot;C:\temp\GatewayDetails_2.csv&quot;
  29. $gatewayDetailsArray | Export-Csv -Path $csvFilePath2 -NoTypeInformation

Output:

获取其他属性的扩展属性

In csv, file got exported like below:

获取其他属性的扩展属性


  1. # $subscriptions = Get-AzSubscription
  2. # Initialize an array to store gateway details
  3. $gatewayDetailsArray = @()
  4. # Loop through each subscription
  5. #foreach ($subscription in $subscriptions) {
  6. # Write-Host &quot;Processing Subscription: $($subscription.Name)&quot; -ForegroundColor Yellow
  7. # Select the current subscription
  8. # Select-AzSubscription -SubscriptionId $subscription.Id
  9. # &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;
  10. $resourceGroups = Get-AzResourceGroup
  11. # Loop through each resource group to get virtual network gateways
  12. foreach ($resourceGroup in $resourceGroups) {
  13. $gateways = Get-AzVirtualNetworkGateway -ResourceGroupName $resourceGroup.ResourceGroupName | Select-Object -ExpandProperty Name
  14. # &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;
  15. foreach ($gateway in $gateways) {
  16. $details = @{
  17. Subscription = $subscription.Name
  18. ResourceGroupName = $resourceGroup.ResourceGroupName
  19. Name = $gateway
  20. vpnType = $gateway.VpnType
  21. GatewayType = $gateway.GatewayType
  22. SKU = $gateway.Sku.Name
  23. VNET = ($gateway.subnet.id -split &#39;/&#39;)[-3]
  24. }
  25. $gatewayDetailsArray += New-Object PSObject -Property $details
  26. }
  27. }
  28. #}
  29. # Output the gateway details
  30. $gatewayDetailsArray | Format-Table -AutoSize
  31. $csvFilePath2 = &quot;C:\temp\GatewayDetails_2.csv&quot;
  32. $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:

  1. # Initialize an array to store gateway details
  2. $gatewayDetailsArray = @()
  3. # Get all subscriptions
  4. $subscriptions = Get-AzSubscription
  5. # Loop through each subscription
  6. foreach ($subscription in $subscriptions) {
  7. Write-Host &quot;Processing Subscription: $($subscription.Name)&quot; -ForegroundColor Yellow
  8. # Select the current subscription
  9. Select-AzSubscription -SubscriptionId $subscription.Id
  10. # Get all resource groups in the current subscription
  11. $resourceGroups = Get-AzResourceGroup
  12. # Loop through each resource group to get virtual network gateways
  13. foreach ($resourceGroup in $resourceGroups) {
  14. $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}}
  15. # Loop through each virtual network gateway and add details to the array
  16. foreach ($gateway in $gateways) {
  17. $gatewayDetailsArray += $gateway
  18. }
  19. }
  20. }
  21. # Output the gateway details
  22. $gatewayDetailsArray | Format-Table -AutoSize Name, SubscriptionID, ResourceGroupName, VpnType, SKUName, SubnetID, SubnetName
  23. # Export the gateway details array to a CSV file
  24. $csvFilePath2 = &quot;C:\temp\GatewayDetails_2.csv&quot;
  25. $gatewayDetailsArray | Export-Csv -Path $csvFilePath2 -NoTypeInformation

获取其他属性的扩展属性

获取其他属性的扩展属性

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

发表评论

匿名网友

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

确定