Q:

How to get data for a metric for 1 day of timegrain in azure powershell?

I want 90 days azure metrics for vm with timegrain of 1 day i.e 24 hours.
Whem i am trying that it fails with error of bad request.
Get-AzureRMMetric -ResourceId $vm[0].Id -TimeGrain 24:00:00 -MetricName "Percentage CPU" -StartTime (Get-Date).adddays(-90) -EndTime (Get-Date)

Error is as follows
Get-AzureRMMetric : Exception type: ErrorResponseException, Message: Microsoft.Azure.Management.Monitor.Models.ErrorResponseException: 
Operation returned an invalid status code 'BadRequest'
   at Microsoft.Azure.Management.Monitor.MetricsOperations.<ListWithHttpMessagesAsync>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.Management.Monitor.MetricsOperationsExtensions.<ListAsync>d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.Commands.Insights.Metrics.GetAzureRmMetricCommand.ProcessRecordInternal()
   at Microsoft.Azure.Commands.Insights.MonitorCmdletBase.ExecuteCmdlet(), Code: Null, Status code:Null, Reason phrase: Null
At line:1 char:9
+ $metric=Get-AzureRMMetric -ResourceId $vm[0].Id -TimeGrain 24:00:00 - ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Get-AzureRmMetric], PSInvalidOperationException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Insights.Metrics.GetAzureRmMetricCommand

A:

The syntax for powershell -Timegrain is
d.HH:MM:ss

So to get a timespan of 1 day in your request, you would write
-TimeGrain 1.00:00:00

Hope this helps.
Regards, John.

