I am trying to get the maximum value of Data Disk Read Bytes/Sec and Data Disk Write Bytes/Sec over a period of 30days for all the data disks (Premium SSD) in my subscription using Get-AzMetric.
I am getting below error when using the Get-AzMetric command.
Get-AzMetric: Exception type: ErrorResponseException, Message: Microsoft.Azure.Management.Monitor.Models.ErrorResponseException: Operation returned an invalid status code 'BadRequest'
What should be changed? I have to use the DISK ID and not the VM ID and I need to use PowerShell for automating the values.
This is my code:
$write_metricName = "Data Disk Write Bytes/Sec"
$et = Get-Date
$st = (Get-Date).AddDays(-30)
# Resource Id is of data disk
$resourceId = "/subscriptions/XXXXX/resourceGroups/rg-xxxxx/providers/Microsoft.Compute/disks/disk-xxxx-01"
$write_metric_avg = Get-AzMetric -ResourceId $resourceId -StartTime $st -EndTime $et -TimeGrain 00:01:00 -MetricName $write_metricName -DetailedOutput -AggregationType Maximum
If I provide resourceId
of the VM instead of the Data Disk, this works just fine for me.
I am currently running this in Cloudshell from Azure portal. Below are the version details:
PS /home/o365> Get-InstalledModule -Name Az -AllVersions | select Name,Version
Name Version
---- -------
Az 9.4.0
PS /home/o365> $PSVersionTable.PSVersion
Major Minor Patch PreReleaseLabel BuildLabel
----- ----- ----- --------------- ----------
7 3 2
If a disk is attached to a VM and if you want to calculate the maximum values of disk read bytes/sec & write bytes/sec
then you need to use the Metric Name as DataDisk Write Bytes/Sec,Data Disk Read Bytes/Sec for -MetricName
property and also you need to pass resourceId of a VM to -resourceId
property in Get-AzMetric cmdlet.
Based on the shared description, we understand that you want to pull the maximum values of Disk Read Bytes/sec
and Disk Write Bytes/sec
metrics (using Get-AzMetric) for all data disks(premium only) that are attached to a VM without passing the resourceId of VM you can use the below script.
The below script will give you the list of data disks attached to different VM's under the subscription and will validate whether that particular disk Sku tier is premium tier or not and will further capture the metrics of disk accordingly.
We have tested the above script in our local environment it is working fine and would suggest you validate from your end.
Here is the Sample PowerShell Script:
$read_metricName = "Composite Disk Read Bytes/sec"
$write_metricName = "Composite Disk Write Bytes/sec"
$et = Get-Date
$st = (Get-Date).AddDays(-30)
$vmlist=Get-AzVM #pulling list of VM under subscription
foreach( $item in $vmlist)
{
#list only data disk Names associated with a specific VM
$disks=$item.StorageProfile.dataDisks.Name
foreach( $disk in $disks){
#pulling repective diskId's
$rdids=Get-AzDisk -Name $disk
foreach( $id in $rdids){
#checking whether the data disk is premium tier or not
if( $id.Sku.Tier -eq "Premium"){
#Write-Host $id.Id
$read_metric_avg = Get-AzMetric -ResourceId $id.Id -StartTime $st -EndTime $et -TimeGrain 00:01:00 -DetailedOutput -AggregationType Maximum -MetricName $read_metricName
$write_metric_avg=Get-AzMetric -ResourceId $id.Id -StartTime $st -EndTime $et -TimeGrain 00:01:00 -DetailedOutput -AggregationType Maximum -MetricName $write_metricName
}
}
}
}
You can use this REST API to know more information about Azure monitor metric definition for a particular resource.