I am at the moment running the following Powershell scipt pull out metrics from Azure
` #login to azure and set context
$Resource = Get-AzResource -ResourceName <appservicename> -ResourceGroupName <resourcegroup> - ResourceType "Microsoft.Web/sites"
$ResourceID = $Resource.ResourceId
$MetricsDefinition = Get-AzMetricDefinition -ResourceId $ResourceID
$MetricsDefinitionHash = @{}
$MetricsDefinition | % {
$Name = $_.Name
$Unit = $_.Unit
$MetricsDefinitionHash.Add($Name,$Unit)
}
$TotalResult = @()
$Metrics = Get-AzMetric -ResourceId $ResourceID -StartTime (Get-date).AddHours(-24) -TimeGrain 00:01:00
$Metrics | % {
if($_.Data -ne $null)
{
$Calc, $metricName, $Result = $Null
$i=0;
$MetricName = $_.Name
$_.Data | % {
$calc += $_.Average
$i++
}
$Result = $Calc/$i
$MetricUnit = $MetricsDefinitionHash["$MetricName"]
$MetricResult = New-Object PSObject
$MetricResult | add-member Noteproperty MetricName $metricName
$MetricResult | add-member Noteproperty MetricValue $Result
$MetricResult | add-member Noteproperty MetricUnit $MetricUnit
$TotalResult += $MetricResult
}
}
`
I expected a result of
MetricName MetricValue MetricUnit
AverageResponseTime 0.7866443 Seconds
AverageMemoryWorkingSet 80747385.625 Bytes
MemoryWorkingSet
Http5xx
Http4xx
Http406
Http404
Http403
Http401
Http3xx 0
Http2xx 0
BytesSent 4699.123
BytesReceived 7.8
Requests 9.4444
CpuTime 0.1111
I have left some values empty , but you get the idea. The issue is im only getting CpuTime . Any ideas on why i would only be getting that ?
I have tried in my environments to get those metrics
$Metrics = Get-AzMetric -ResourceId $ResourceID -StartTime (Get-date).AddHours(-24) -TimeGrain 00:01:00
The above returns variable $Metrics returns only metrics of CpuTime and its details like Id, name, type ,unit ,Data,timeseries as it is the first metric that appearsWith Id :
/subscriptions/xxxxxx23f/resourceGroups/xxxxx/providers/Microsoft.Web/sites/metricdemoapp/providers/Microsoft.Insights/metrics/CpuTimeAfter Checking the supported metrics in portal in my case , web app service
when tried with other metric name, got the units as required.
Where as below command for definition gives details of all metrics along with details one by one.
$MetricsDefinition = Get-AzMetricDefinition -ResourceId $ResourceID $MetricsDefinition
Powershell :
Output:
Will be something like:
And continues for every metric like below
Where AverageResponseTime is one of the metrics you can see
EDIT: 28/11/2022
Try adding aggregation type Average lie
$MetricDetails= Get-AzMetric -ResourceId $ResourceID -MetricName $MetricName -AggregationType Average -TimeGrain 06:00:00