英文:
Getting extended properties with other properties
问题
我有一个 PowerShell 脚本,用于收集所有 Az 虚拟网络网关的信息,但似乎在获取资源名称时出现了问题。我得到的是带有红色箭头的截图中的名称,而不是带有黄色箭头的名称。
$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 "Processing Subscription: $($subscription.Name)" -ForegroundColor Yellow
# 选择当前订阅
# Select-AzSubscription -SubscriptionId $subscription.Id
# <span class=" active-doc-0" data-doc-items="0">获取当前订阅中的所有资源组[1](#doc-pos=0)</span>
$resourceGroups = Get-AzResourceGroup
# 循环遍历每个资源组以获取虚拟网络网关
foreach ($resourceGroup in $resourceGroups) {
$gateways = Get-AzVirtualNetworkGateway -ResourceGroupName $resourceGroup.ResourceGroupName | Select-Object -ExpandProperty Name
# <span class=" active-doc-1" data-doc-items="1">循环遍历每个虚拟网络网关并将详细信息添加到数组中[2](#doc-pos=1)</span>
foreach ($gateway in $gateways) {
$details = @{
Subscription = $subscription.Name
ResourceGroupName = $resourceGroup.ResourceGroupName
Name = $gateway
}
$gatewayDetailsArray += New-Object PSObject -Property $details
}
}
#}
# 输出网关详细信息
$gatewayDetailsArray | Format-Table -AutoSize
# 将网关详细信息数组导出到CSV文件
$csvFilePath2 = "C:\temp\GatewayDetails_2.csv"
$gatewayDetailsArray | Export-Csv -Path $csvFilePath2 -NoTypeInformation
输出:
CSV文件导出的内容如下:
# $subscriptions = Get-AzSubscription
# 初始化一个数组来存储网关详细信息
$gatewayDetailsArray = @()
# 循环遍历每个订阅
#foreach ($subscription in $subscriptions) {
# Write-Host "Processing Subscription: $($subscription.Name)" -ForegroundColor Yellow
# 选择当前订阅
# Select-AzSubscription -SubscriptionId $subscription.Id
# <span class=" active-doc-0" data-doc-items="0">获取当前订阅中的所有资源组[1](#doc-pos=0)</span>
$resourceGroups = Get-AzResourceGroup
# 循环遍历每个资源组以获取虚拟网络网关
foreach ($resourceGroup in $resourceGroups) {
$gateways = Get-AzVirtualNetworkGateway -ResourceGroupName $resourceGroup.ResourceGroupName | Select-Object -ExpandProperty Name
# <span class=" active-doc-1" data-doc-items="1">循环遍历每个虚拟网络网关并将详细信息添加到数组中[2](#doc-pos=1)</span>
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 '/')[-3]
}
$gatewayDetailsArray += New-Object PSObject -Property $details
}
}
#}
# 输出网关详细信息
$gatewayDetailsArray | Format-Table -AutoSize
$csvFilePath2 = "C:\temp\GatewayDetails_2.csv"
$gatewayDetailsArray | Export-Csv -Path $csvFilePath2 -NoTypeInformation
更新
> 我们希望输出以下内容。网关名称、订阅ID、资源组、VPN类型、SKU名称、来自IP配置的子网ID(参考原始截图),以便我们可以提取VNET详细信息。
您可以使用以下命令获取所有详细信息,如下所示:
# 初始化一个数组来存储网关详细信息
$gatewayDetailsArray = @()
# 获取所有订阅
$subscriptions = Get-AzSubscription
# 循环遍历每个订阅
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, @{Name="SubscriptionID";Expression={$subscription.Id}}, ResourceGroupName, VpnType, @{Name="SKUName";Expression={$_.Sku.Name}}, @{Name="SubnetID";Expression={$_.IpConfigurations.Subnet.Id}}, @{Name="SubnetName";Expression={(Get-AzVirtualNetworkSubnetConfig -ResourceId $_.IpConfigurations.Subnet.Id).Name}}
# 循环遍历每个虚拟网络网关并将详细信息添加到数组中
foreach ($gateway in $gateways) {
$gatewayDetailsArray += $gateway
}
}
}
# 输出网关详细信息
$gatewayDetailsArray | Format-Table -AutoSize Name, SubscriptionID, ResourceGroupName, VpnType, SKUName, SubnetID, SubnetName
# 将网关详细信息数组导出到CSV文件
$csvFilePath2 = "C:\temp\GatewayDetails_2.csv"
$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 "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
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 "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
vpnType = $gateway.VpnType
GatewayType = $gateway.GatewayType
SKU = $gateway.Sku.Name
VNET = ($gateway.subnet.id -split '/')[-3]
}
$gatewayDetailsArray += New-Object PSObject -Property $details
}
}
#}
# Output the gateway details
$gatewayDetailsArray | Format-Table -AutoSize
$csvFilePath2 = "C:\temp\GatewayDetails_2.csv"
$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 "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, @{Name="SubscriptionID";Expression={$subscription.Id}}, ResourceGroupName, VpnType, @{Name="SKUName";Expression={$_.Sku.Name}}, @{Name="SubnetID";Expression={$_.IpConfigurations.Subnet.Id}}, @{Name="SubnetName";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 = "C:\temp\GatewayDetails_2.csv"
$gatewayDetailsArray | Export-Csv -Path $csvFilePath2 -NoTypeInformation
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论