尝试获取CPU利用率(最好是网络利用率)。我也很想获得RAM,但据我所知,这需要安装来宾模块才能获得这些指标。因此,在这一点上,我只需要“主机”级别的指标)。
我的想法是对订阅中的所有虚拟机运行此命令,以获取虚拟机名称、虚拟机资源组、过去x天的CPU利用率、过去x天的网络输入和过去x天的网络输出。
我尝试的第一件事是,使用"Get-AzureRMMetric",开始给出错误。
我键入" get -azurermmetric",系统提示输入资源ID。我输入虚拟机的资源ID,得到的响应是一长串警告和异常类型,表明返回了无效的状态代码“notfound”。
有什么想法吗?
发布于 2019-01-25 16:48:56
首先,您需要确定vm支持哪些指标,请使用以下代码:
(Get-AzureRmMetricDefinition -ResourceId "vm resource id").name然后,您可以看到支持的指标(只需忽略警告消息):

根据您的问题,我认为您需要“百分比CPU”/“网络输入”/“网络输出”。
然后,您可以使用下面的示例代码(如果它不能满足您的需要,您可以进行一些更改):
#get all vms in a resource group, but you can remove -ResourceGroupName "xxx" to get all the vms in a subscription
$vms = Get-AzureRmVM -ResourceGroupName "xxx"
#get the last 3 days data
#end date
$et=Get-Date
#start date
$st=$et.AddDays(-3)
#define an array to store the infomation like vm name / resource group / cpu usage / network in / networkout
$arr =@()
foreach($vm in $vms)
{
#define a string to store related infomation like vm name etc. then add the string to an array
$s = ""
#percentage cpu usage
$cpu = Get-AzureRmMetric -ResourceId $vm.Id -MetricName "Percentage CPU" -DetailedOutput -StartTime $st `
-EndTime $et -TimeGrain 12:00:00 -WarningAction SilentlyContinue
#network in
$in = Get-AzureRmMetric -ResourceId $vm.Id -MetricName "Network In" -DetailedOutput -StartTime $st `
-EndTime $et -TimeGrain 12:00:00 -WarningAction SilentlyContinue
#network out
$out = Get-AzureRmMetric -ResourceId $vm.Id -MetricName "Network Out" -DetailedOutput -StartTime $st `
-EndTime $et -TimeGrain 12:00:00 -WarningAction SilentlyContinue
# 3 days == 72hours == 12*6hours
$cpu_total=0.0
$networkIn_total = 0.0
$networkOut_total = 0.0
foreach($c in $cpu.Data.Average)
{
#this is a average value for 12 hours, so total = $c*12 (or should be $c*12*60*60)
$cpu_total += $c*12
}
foreach($i in $in.Data.total)
{
$networkIn_total += $i
}
foreach($t in $out.Data.total)
{
$networkOut_total += $t
}
# add all the related info to the string
$s = "VM Name: " + $vm.name + "; Resource Group: " + $vm.ResourceGroupName + "; CPU: " +$cpu_total +"; Network In: " + $networkIn_total + "; Network Out: " + $networkOut_total
# add the above string to an array
$arr += $s
}
#check the values in the array
$arr测试结果:

发布于 2020-02-11 18:47:54
空格
就是我们可以使用相同的脚本来获取CPU的最大使用率,我已经习惯了下面的更改,比如调用maximum ...no luck..我希望有一些条件需要你有解决方案
foreach($c in $cpu.Data.Maximum)
{
#this is a average value for 12 hours, so total = $c*12 (or should be $c*12*60*60)
$cpu_total += $c*12
#($c|measure -maximum).maximum
}https://stackoverflow.com/questions/54312696
复制相似问题