如何在vROps/Aria中为指定VM配置PDF格式性能报表?
解决vROPS虚拟机CPU指标报表时间对齐问题并生成PDF
你遇到的核心问题有两个:一是原脚本里$last30days = $today.AddDays(-30).多了个语法错误的句号;二是vROPS不同CPU指标的采样时间戳可能不完全一致,直接把Value数组塞进自定义对象会因为数组长度不匹配报错。另外你最终需要PDF报表,原脚本仅生成CSV,也需要补充转PDF的逻辑。
PowerShell修复方案(含时间对齐+PDF生成)
先修正基础语法,再通过时间戳对齐所有指标数据,最后借助Excel转PDF生成报表。需要提前安装依赖模块:
Install-Module -Name VMware.PowerCLI -Scope CurrentUser Install-Module -Name ImportExcel -Scope CurrentUser
修正后的完整脚本:
$resource = 'Server01' $vropsServer = 'vrops.example.com' $cred = Get-Credential # 连接vROPS服务器 try { Connect-OMServer -Server $vropsServer -Credential $cred -ErrorAction Stop | Out-Null Write-Host "已连接到vROPS服务器" } catch { Write-Host "连接vROPS失败: $($_.Exception.Message)" exit 1 } # 修正日期变量语法错误 $today = Get-Date $last30days = $today.AddDays(-30) # 获取所有指标数据(保留时间戳,便于后续对齐) $metricsData = @( Get-OMStat -Resource $resource -Key 'cpu|demandmhz' -From $last30days -To $today -ErrorAction Stop | Select-Object Timestamp, @{Name='DemandMhz'; Expression={$_.Value}} Get-OMStat -Resource $resource -Key 'cpu|demandPct' -From $last30days -To $today -ErrorAction Stop | Select-Object Timestamp, @{Name='DemandPct'; Expression={$_.Value}} Get-OMStat -Resource $resource -Key 'cpu|usage_average' -From $last30days -To $today -ErrorAction Stop | Select-Object Timestamp, @{Name='UsageAveragePct'; Expression={$_.Value}} Get-OMStat -Resource $resource -Key 'cpu|usagemhz_average' -From $last30days -To $today -ErrorAction Stop | Select-Object Timestamp, @{Name='UsageAverageMhz'; Expression={$_.Value}} ) # 按时间戳合并所有指标数据(仅保留所有指标都存在的时间点) $mergedData = $metricsData | Group-Object Timestamp | ForEach-Object { $row = [PSCustomObject]@{ Timestamp = $_.Name Resource = $resource } $_.Group | ForEach-Object { $props = $_.PSObject.Properties | Where-Object Name -ne 'Timestamp' foreach ($prop in $props) { $row | Add-Member -NotePropertyName $prop.Name -NotePropertyValue $prop.Value } } $row } # 导出为格式化Excel $excelPath = "C:\temp\cpu_report.xlsx" $mergedData | Export-Excel -Path $excelPath -AutoSize -BoldTopRow # 将Excel转换为PDF $excelApp = New-Object -ComObject Excel.Application $excelApp.Visible = $false $workbook = $excelApp.Workbooks.Open($excelPath) $pdfPath = "C:\temp\cpu_report.pdf" $workbook.ExportAsFixedFormat([Microsoft.Office.Interop.Excel.XlFixedFormatType]::xlTypePDF, $pdfPath) $workbook.Close() $excelApp.Quit() # 清理COM对象避免内存泄漏 [System.Runtime.Interopservices.Marshal]::ReleaseComObject($workbook) | Out-Null [System.Runtime.Interopservices.Marshal]::ReleaseComObject($excelApp) | Out-Null [GC]::Collect() [GC]::WaitForPendingFinalizers() Write-Host "PDF报表已生成: $pdfPath" # 断开vROPS连接 Disconnect-OMServer -Server $vropsServer -Confirm:$false
关键修复点说明
- 修正了日期变量的语法错误
- 每个指标都保留
Timestamp,通过分组合并确保同一时间点的所有指标值一一对应 - 用
Export-Excel生成规整的Excel文件,再通过Excel COM对象转PDF - 清理COM对象避免内存残留
可选REST API方案(无PowerCLI依赖)
如果不想依赖PowerCLI模块,可直接调用vROPS REST API获取指标,后续合并数据和生成PDF的逻辑与上述一致:
- 获取目标虚拟机的资源ID
$authHeader = @{Authorization = "Basic $([Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$($cred.UserName):$($cred.GetNetworkCredential().Password)")))"} $resourceId = (Invoke-RestMethod -Uri "https://$vropsServer/suite-api/api/resources?name=$resource" -Headers $authHeader -Method Get -SkipCertificateCheck).resourceList.id
- 获取所有CPU指标数据(转换时间戳为本地时间)
$fromUnix = [Math]::Floor((Get-Date $last30days -UFormat %s)) * 1000 $toUnix = [Math]::Floor((Get-Date $today -UFormat %s)) * 1000 $metricsKeys = 'cpu|demandmhz','cpu|demandPct','cpu|usage_average','cpu|usagemhz_average' $metricData = @() foreach ($key in $metricsKeys) { $uri = "https://$vropsServer/suite-api/api/resources/$resourceId/stats?statKey=$key&startTime=$fromUnix&endTime=$toUnix" $data = Invoke-RestMethod -Uri $uri -Headers $authHeader -Method Get -SkipCertificateCheck $metricData += $data.statisticSamples.sample | Select-Object @{Name='Timestamp'; Expression={[DateTimeOffset]::FromUnixTimeMilliseconds($_.timestamp).LocalDateTime}}, @{Name=$key.Replace('|','_'); Expression={$_.value}} }
- 后续按时间戳合并数据、生成Excel和PDF的逻辑与PowerShell方案完全一致。
内容的提问来源于stack exchange,提问作者SpicerLabs
相关产品推荐
相关产品推荐

