解决Azure PowerShell中Get-AzMetric获取App Service指标的类型转换错误
解决Get-AzMetric聚合类型参数转换错误问题
问题原因
你遇到的错误源于两个核心问题:
- 脚本使用了错误的参数名:
-Aggregation,而Get-AzMetric的正确参数名为-AggregationType -AggregationType参数仅支持传入单个AggregationType枚举值,无法直接接受数组类型的多个聚合值,PowerShell无法将数组转换为参数要求的可空枚举类型,因此抛出转换错误。
另外,你的脚本未切换到指定订阅,需添加订阅切换命令确保后续操作在目标订阅下执行。
解决方案
要获取多个聚合类型的指标,需要为每个聚合类型单独调用Get-AzMetric,或者合并不同聚合类型的结果。以下是两种可行的修改方案:
方案1:按聚合类型分别输出指标
该方案循环遍历指定的聚合类型,分别获取并输出每个类型的指标数据:
# Connect to Azure account Connect-AzAccount -UseDeviceAuthentication # Specify the subscription ID $subscriptionId = "<subscription-id>" # Switch to the target subscription Set-AzContext -Subscription $subscriptionId # Get all App Services in the subscription $appServices = Get-AzWebApp # Specify the time range for metrics retrieval $startTime = (Get-Date).AddDays(-90) $endTime = Get-Date # Define aggregation types to retrieve $aggregations = @("Maximum", "Minimum", "Average") # Iterate through each App Service and retrieve metrics foreach ($appService in $appServices) { $appServiceName = $appService.Name $resourceGroup = $appService.ResourceGroup Write-Host "`nRetrieving metrics for App Service: $appServiceName" # Retrieve CPU metrics for each aggregation type Write-Host "`nCPU Metrics:" foreach ($agg in $aggregations) { $cpuMetrics = Get-AzMetric -ResourceId $appService.Id ` -StartTime $startTime ` -EndTime $endTime ` -MetricNames "CpuPercentage" ` -AggregationType $agg $cpuMetrics.Data | Select-Object Timestamp, @{Name=$agg; Expression={$_.$agg}} } # Retrieve memory metrics for each aggregation type Write-Host "`nMemory Metrics:" foreach ($agg in $aggregations) { $memoryMetrics = Get-AzMetric -ResourceId $appService.Id ` -StartTime $startTime ` -EndTime $endTime ` -MetricNames "MemoryPercentage" ` -AggregationType $agg $memoryMetrics.Data | Select-Object Timestamp, @{Name=$agg; Expression={$_.$agg}} } }
方案2:合并同一时间戳的多聚合值
该方案将同一时间戳的最大、最小、平均值合并到同一对象中输出,更贴近你原本的预期格式:
# Connect to Azure account Connect-AzAccount -UseDeviceAuthentication # Specify the subscription ID $subscriptionId = "<subscription-id>" # Switch to the target subscription Set-AzContext -Subscription $subscriptionId # Get all App Services in the subscription $appServices = Get-AzWebApp # Specify the time range for metrics retrieval $startTime = (Get-Date).AddDays(-90) $endTime = Get-Date # Iterate through each App Service and retrieve metrics foreach ($appService in $appServices) { $appServiceName = $appService.Name $resourceGroup = $appService.ResourceGroup Write-Host "`nRetrieving metrics for App Service: $appServiceName" # Retrieve CPU metrics with all aggregations $cpuMax = Get-AzMetric -ResourceId $appService.Id ` -StartTime $startTime ` -EndTime $endTime ` -MetricNames "CpuPercentage" ` -AggregationType Maximum $cpuMin = Get-AzMetric -ResourceId $appService.Id ` -StartTime $startTime ` -EndTime $endTime ` -MetricNames "CpuPercentage" ` -AggregationType Minimum $cpuAvg = Get-AzMetric -ResourceId $appService.Id ` -StartTime $startTime ` -EndTime $endTime ` -MetricNames "CpuPercentage" ` -AggregationType Average # Combine CPU metrics into single objects $combinedCpuMetrics = for ($i=0; $i -lt $cpuMax.Data.Count; $i++) { [PSCustomObject]@{ Timestamp = $cpuMax.Data[$i].Timestamp Maximum = $cpuMax.Data[$i].Maximum Minimum = $cpuMin.Data[$i].Minimum Average = $cpuAvg.Data[$i].Average } } Write-Host "`nCPU Metrics:" $combinedCpuMetrics # Retrieve and combine Memory metrics $memoryMax = Get-AzMetric -ResourceId $appService.Id ` -StartTime $startTime ` -EndTime $endTime ` -MetricNames "MemoryPercentage" ` -AggregationType Maximum $memoryMin = Get-AzMetric -ResourceId $appService.Id ` -StartTime $startTime ` -EndTime $endTime ` -MetricNames "MemoryPercentage" ` -AggregationType Minimum $memoryAvg = Get-AzMetric -ResourceId $appService.Id ` -StartTime $startTime ` -EndTime $endTime ` -MetricNames "MemoryPercentage" ` -AggregationType Average $combinedMemoryMetrics = for ($i=0; $i -lt $memoryMax.Data.Count; $i++) { [PSCustomObject]@{ Timestamp = $memoryMax.Data[$i].Timestamp Maximum = $memoryMax.Data[$i].Maximum Minimum = $memoryMin.Data[$i].Minimum Average = $memoryAvg.Data[$i].Average } } Write-Host "`nMemory Metrics:" $combinedMemoryMetrics }
额外注意事项
- 确保安装最新版本的Az.Monitor模块,执行
Update-Module -Name Az.Monitor完成更新 - 部分App Service可能无有效指标数据,脚本会输出空结果,属于正常情况
内容的提问来源于stack exchange,提问作者mo taar
相关产品推荐
相关产品推荐

