Skip to content

Instantly share code, notes, and snippets.

@briglx
Last active October 2, 2019 16:49
Show Gist options
  • Save briglx/fefb69e4c3f440ca169bb89d85422186 to your computer and use it in GitHub Desktop.
Save briglx/fefb69e4c3f440ca169bb89d85422186 to your computer and use it in GitHub Desktop.
Get VM CPU for the past week
Connect-AzAccount
$vms = Get-AzVm
$Report = @()
foreach ($vm in $vms) {
$rid = $vm.id
$cpu = Get-AzMetric -ResourceId $rid -TimeGrain 00:15:00 -StartTime 2019-09-22T12:00:00Z -EndTime 2019-09-28T23:59:59Z -WarningAction silentlyContinue
$curAvg = 0
$max = 0
$n = 1
foreach ($c in $cpu.data){
if($c.average -gt $max){
$max = $c.average
}
if($c.average -gt 0){
$curAvg = $curAvg + ($c.average - $curAvg)/$n
$n = $n + 1
}
}
$Object = New-Object System.Object
$Object | Add-Member -type NoteProperty -name "Id" -Value $rid
$Object | Add-Member -type NoteProperty -name "AveCpu" -Value $curAvg
$Object | Add-Member -type NoteProperty -name "MaxCpu" -Value $max
$Object | Add-Member -type NoteProperty -name "Count" -Value $n
if($curAvg -gt 0){
$Report += $Object
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment