You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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:

Using Az PowerShell Module

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.
Using Azure CLI

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-AzAccount for the Az module, or az login for Azure CLI.
  • If your tags contain special characters (spaces, symbols), Export-Csv will 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 20:32:49