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

如何用PowerShell导出近7天Azure SQL数据库多指标Excel性能报告

Azure SQL数据库7天性能报告PowerShell实现方案

前置环境准备

  • 安装所需PowerShell模块,执行以下命令即可完成依赖安装:
    Install-Module -Name Az.Accounts,Az.Sql,Az.Monitor,ImportExcel -Scope CurrentUser -Force
  • 执行账号需持有所有目标Azure订阅的监控读取者及以上权限,确保可以正常拉取资源监控数据

完整执行脚本

# 1. 登录Azure账号
Connect-AzAccount

# 2. 定义统计参数
$startTime = (Get-Date).AddDays(-7)
$endTime = Get-Date
$timeGrain = [TimeSpan]::FromHours(1) # 可修改为[TimeSpan]::FromDays(1)按天聚合减少数据量
$exportPath = "C:\AzureSQL_Perf_Report_$($endTime.ToString('yyyyMMdd')).xlsx"

# 3. 初始化结果存储数组
$perfResults = @()

# 4. 遍历所有订阅采集指标
$allSubscriptions = Get-AzSubscription
foreach ($sub in $allSubscriptions) {
    # 切换当前订阅上下文
    Set-AzContext -Subscription $sub.Id | Out-Null
    Write-Host "正在处理订阅:$($sub.Name)"

    # 拉取当前订阅下所有SQL服务器
    $allSqlServers = Get-AzSqlServer
    foreach ($server in $allSqlServers) {
        Write-Host "正在处理SQL服务器:$($server.ServerName)"

        # 拉取当前服务器下所有用户数据库,排除系统库master
        $allDatabases = Get-AzSqlDatabase -ServerName $server.ServerName -ResourceGroupName $server.ResourceGroupName | Where-Object {$_.DatabaseName -ne "master"}
        foreach ($db in $allDatabases) {
            Write-Host "正在采集数据库:$($db.DatabaseName) 指标"

            # 定义需要拉取的核心指标列表
            $metrics = @("cpu_percent", "dtu_consumption_percent", "deadlock", "connection_failed")
            foreach ($metric in $metrics) {
                # 跳过vCore模型数据库无DTU指标的场景
                if ($metric -eq "dtu_consumption_percent" -and $db.Sku.Tier -in @("GeneralPurpose", "BusinessCritical", "Hyperscale")) {
                    continue
                }

                # 拉取指标数据,默认取时间窗口最大值,可按需调整聚合类型
                $metricData = Get-AzMetric -ResourceId $db.Id -MetricName $metric -StartTime $startTime -EndTime $endTime -TimeGrain $timeGrain -AggregationType Maximum

                # 遍历每个时间点的指标值写入结果集
                foreach ($dataPoint in $metricData.Data) {
                    if ($null -ne $dataPoint.Maximum) {
                        $perfResults += [PSCustomObject]@{
                            订阅名称 = $sub.Name
                            资源组名称 = $server.ResourceGroupName
                            SQL服务器名称 = $server.ServerName
                            数据库名称 = $db.DatabaseName
                            统计时间 = $dataPoint.TimeStamp
                            指标名称 = switch ($metric) {
                                "cpu_percent" {"CPU使用率(%)"}
                                "dtu_consumption_percent" {"DTU使用率(%)"}
                                "deadlock" {"死锁数"}
                                "connection_failed" {"失败连接数"}
                            }
                            指标最大值 = [math]::Round($dataPoint.Maximum,2)
                        }
                    }
                }
            }
        }
    }
}

# 5. 导出为Excel文件,自动适配列宽、冻结首行、开启筛选
$perfResults | Export-Excel -Path $exportPath -WorksheetName "SQL性能指标" -AutoSize -FreezeTopRow -AutoFilter -Show

自定义调整说明

  • 聚合规则修改:脚本默认取每个时间窗口的最大值统计,可修改Get-AzMetric的AggregationType参数为Average/Minimum获取平均值/最小值
  • 阈值高亮配置:可新增条件格式对超标指标标红,示例如下:
    $cond = New-ConditionalText -Range 'G:G' -ConditionalType GreaterThan 80 -BackgroundColor Red -ForegroundColor White
    $perfResults | Export-Excel -Path $exportPath -ConditionalText $cond -WorksheetName "SQL性能指标" -AutoSize -FreezeTopRow -AutoFilter -Show
    
  • 订阅过滤:如果仅需统计部分订阅,可修改$allSubscriptions = Get-AzSubscription | Where-Object {$_.Name -match "生产"}按名称过滤

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 03:24:07