如何在azure powershell中获取1天时间粒度的指标数据?

628mspwn  于 2023-01-14  发布在  Shell
关注(0)|答案(3)|浏览(129)

我想要90天的时间粒度为1天,即24小时的虚拟机Azure指标。当我尝试,它与错误的请求失败。

Get-AzureRMMetric -ResourceId $vm[0].Id -TimeGrain 24:00:00 -MetricName "Percentage CPU" -StartTime (Get-Date).adddays(-90) -EndTime (Get-Date)

错误如下

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
fsi0uk1n

fsi0uk1n1#

powershell -Timegrain的语法为

d.HH:MM:ss

因此,要在请求中获得1天的时间跨度,您可以编写

-TimeGrain 1.00:00:00

希望这能帮上忙问候你约翰。

shyt4zoc

shyt4zoc2#

当它显示BadRequest时,很可能你传递了一些错误的参数。我看到,你调用Id来传递resourceId,我怀疑可能有问题。
请查看以下详细信息,这些信息将为您提供度量标准:

# Name of your VM or
$vmName = 'VM NAME'

# Get all the resources
$allResource = Get-AzureRmResource

# Filter your resource based on name
$specificVM= $allResource | Where-Object {$_.Name -eq $vmName}
ECHO $specificVM

# Retrieve the data
$detail = Get-AzureRMMetric -ResourceId $specificVM.ResourceId -TimeGrain 24:00:00 -MetricName "Percentage CPU" -StartTime (Get-Date).adddays(-90) -EndTime (Get-Date)
ECHO $detail

# Get the data
ECHO $detail.Data

mwg9r5ms

mwg9r5ms3#

这对我来说绝对有效:

$ResourceId = <my resourceId>
(Get-AzMetric -ResourceId $ResourceI -TimeGrain 1.00:00:00).Data
    • 产出**
TimeStamp : 27.09.2021 13:25:00
Average   :
Minimum   :
Maximum   :
Total     : 8258
Count     :

在PowerShell 7下测试:

PS C:\Windows\System32> Get-Module -Name Az.Monitor

ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Script     2.7.0                 Az.Monitor

如果您想在某一天(比如昨天)使用:

(Get-AzMetric -ResourceId $ResourceI -TimeGrain 1.00:00:00 -StartTime (Get-Date).AddDays(-1)).Data

相关问题