我有一个 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
要收集订阅中所有虚拟网关的信息,您可以使用以下修改后的脚本:
# $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
输出:
在 csv 中,文件导出如下: