PowerShell新手求助:自动导出带标签的Azure资源组至CSV
Hey there! Let's tackle this problem step by step. You want to export all Azure resource groups including all their tags automatically (without manually specifying each tag) to a CSV, matching the format from your first example. Here are two robust approaches using Az PowerShell and Azure CLI, along with the reasoning behind each:
The key here is to dynamically detect all unique tag keys across your resource groups, then build your output properties automatically instead of hardcoding tag names.
# 1. Fetch all resource groups with their full data (including tags) $allResourceGroups = Get-AzResourceGroup # 2. Collect every unique tag key used across all resource groups $allUniqueTagKeys = $allResourceGroups.Tags.Keys | Select-Object -Unique # 3. Build a list of properties to select: default RG properties + dynamic tag columns $selectProperties = @( # Core resource group properties 'ResourceGroupName', 'Location', 'ResourceId', 'ProvisioningState', # Add each tag as a calculated property (with "(Tag)" suffix like your example) $allUniqueTagKeys | ForEach-Object { @{ Name = "$_ (Tag)" Expression = { $_.Tags[$_] } # Pull the tag value for each RG } } ) # 4. Select properties and export to CSV $allResourceGroups | Select-Object -Property $selectProperties | Export-Csv -Path "ResourceGroups_AllTags.csv" -NoTypeInformation
How this works:
- We first gather all resource groups so we can scan their tags.
- By collecting all unique tag keys, we avoid missing any tags that might exist on only some resource groups.
- The dynamic calculated properties ensure each tag becomes its own column in the CSV, just like your manual example—but without having to update the script when new tags are added.
- Resource groups missing a specific tag will have an empty cell in that CSV column.
The default CLI output puts tags in a single object column, which doesn't work well for CSV. We'll process the CLI output to expand tags into individual columns matching your desired format.
# 1. Get resource group data via CLI and convert to PowerShell objects $cliResourceGroups = az group list --query "[].{ResourceGroupName:name, Location:location, ResourceId:id, ProvisioningState:properties.provisioningState, Tags:tags}" -o json | ConvertFrom-Json # 2. Process each RG to expand tags into separate properties $processedRGs = $cliResourceGroups | ForEach-Object { $rg = $_ # Start with the core RG properties $output = [PSCustomObject]@{ ResourceGroupName = $rg.ResourceGroupName Location = $rg.Location ResourceId = $rg.ResourceId ProvisioningState = $rg.ProvisioningState } # Add each tag as a dedicated property (with "(Tag)" suffix) if ($rg.Tags) { $rg.Tags.GetEnumerator() | ForEach-Object { $output | Add-Member -MemberType NoteProperty -Name "$($_.Key) (Tag)" -Value $_.Value } } $output } # 3. Export the processed data to CSV $processedRGs | Export-Csv -Path "ResourceGroups_CLI_AllTags.csv" -NoTypeInformation
How this works:
- The CLI query pulls the exact core properties we need, plus the full tags object.
- We loop through each resource group, create a base object with core properties, then add each tag as a new property.
- This ensures tags are split into individual columns, just like your manual PowerShell example.
Quick Notes:
- Make sure you're logged into Azure first: use
Connect-AzAccountfor the Az module, oraz loginfor Azure CLI. - If your tags contain special characters (spaces, symbols),
Export-Csvwill automatically wrap those values in quotes to keep the CSV valid. - Resource groups without any tags will still appear in the CSV with empty tag columns.
内容的提问来源于stack exchange,提问作者maxi.mdq

