英文:
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 "正在处理订阅:$($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.Name
资源组名称 = $resourceGroup.ResourceGroupName
名称 = $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文件中,导出的内容如下:
更新
> 我们希望输出如下。网关名称,订阅ID,资源组,VPN类型,SKU名称,来自IP配置的子网ID(参考原始截图),以便我们可以提取VNET详细信息
您可以使用以下命令获取所有详细信息如下:
# 初始化一个数组以存储网关详细信息
$gatewayDetailsArray = @()
# 获取所有订阅
$subscriptions = Get-AzSubscription
# 遍历每个订阅
foreach ($subscription in $subscriptions) {
Write-Host "正在处理订阅:$($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="订阅ID";Expression={$subscription.Id}}, ResourceGroupName, VpnType, @{Name="SKU名称";Expression={$_.Sku.Name}}, @{Name="子网ID";Expression={$_.IpConfigurations.Subnet.Id}}, @{Name="子网名称";Expression={(Get-AzVirtualNetworkSubnetConfig -ResourceId $_.IpConfigurations.Subnet.Id).Name}}
# 遍历每个虚拟网络网关并将详细信息添加到数组中
foreach ($gateway in $gateways) {
$gatewayDetailsArray += $gateway
}
}
}
# 输出网关详细信息
$gatewayDetailsArray | Format-Table -AutoSize 名称, 订阅ID, 资源组名称, VPN类型, SKU名称, 子网ID, 子网名称
# 将网关详细信息数组导出到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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论