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

基于标签筛选多订阅Azure存储账户并获取容量的PowerShell需求

Azure全订阅存储账户标签筛选及容量查询脚本

以下是修改后的完整脚本,实现了按指定标签键值对筛选存储账户,并获取已使用存储容量的功能:

# 认证到Azure租户
Connect-AzAccount -Tenant 'xxxxx' -UseDeviceAuthentication

# 获取当前租户下所有订阅
$subscriptions = Get-AzSubscription

# 指定要筛选的标签键和值
$tagName = "business"
$tagValue = "xyz"

# 初始化结果数组
$storageAccountDetails = @()

foreach ($subscription in $subscriptions) {
    Write-Host "正在处理订阅: $($subscription.Name)" -ForegroundColor Cyan
    
    try {
        # 设置当前订阅上下文
        Set-AzContext -SubscriptionId $subscription.Id | Out-Null
        
        # 筛选符合标签条件的存储账户
        $filteredStorageAccounts = Get-AzStorageAccount | Where-Object {
            $_.Tags.ContainsKey($tagName) -and $_.Tags[$tagName] -eq $tagValue
        }

        if (-not $filteredStorageAccounts) {
            Write-Host "该订阅下无符合标签条件的存储账户" -ForegroundColor Gray
            continue
        }

        # 遍历筛选后的存储账户,获取容量信息
        foreach ($sa in $filteredStorageAccounts) {
            # 查询存储账户已使用容量指标(最近30天的最新值)
            $capacityMetric = Get-AzMetric -ResourceId $sa.Id `
                -MetricName "UsedCapacity" `
                -TimeGrain 00:05:00 `
                -StartTime (Get-Date).AddDays(-30) `
                -EndTime (Get-Date) `
                -AggregationType Average

            # 提取最新的容量值(转换为GB)
            $usedCapacityGB = if ($capacityMetric.Data) {
                [math]::Round($capacityMetric.Data[-1].Average / 1GB, 2)
            } else {
                "无数据"
            }

            # 构造结果对象
            $storageAccountDetails += [PSCustomObject]@{
                订阅名称          = $subscription.Name
                资源组名称        = $sa.ResourceGroupName
                存储账户名称      = $sa.StorageAccountName
                区域              = $sa.PrimaryLocation
                账户类型          = $sa.SkuName  # 原AccountType已弃用,改用SkuName
                创建时间          = $sa.CreationTime
                标签              = $sa.Tags | ConvertTo-Json -Compress
                已使用容量(GB)    = $usedCapacityGB
            }
        }
    }
    catch {
        Write-Error "处理订阅 $($subscription.Name) 时出错: $_"
    }
}

# 输出结果
if ($storageAccountDetails) {
    Write-Host "`n查询结果如下:" -ForegroundColor Green
    $storageAccountDetails | Format-Table -AutoSize
    # 可选:导出到CSV文件
    # $storageAccountDetails | Export-Csv -Path ".\存储账户容量报告.csv" -Encoding UTF8 -NoTypeInformation
}
else {
    Write-Host "所有订阅中均未找到符合条件的存储账户" -ForegroundColor Yellow
}

关键修改说明

  1. 标签精准筛选
    添加Where-Object条件,先判断标签键是否存在,再匹配对应值,避免因标签缺失导致的空值报错。

  2. 存储容量获取
    使用Get-AzMetric查询UsedCapacity指标,设置最近30天的时间范围和5分钟粒度,提取最新平均值并转换为易读的GB单位。

  3. 字段更新
    替换已弃用的AccountType字段为SkuName,符合Azure PowerShell最新规范。

  4. 可读性优化

    • 用彩色日志区分不同状态的提示信息
    • 将标签转换为压缩JSON字符串,方便在表格中查看完整标签内容
    • 增加无符合条件账户时的友好提示

内容的提问来源于stack exchange,提问作者Nolan1513

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 11:49:50