Last active
October 2, 2019 16:49
-
-
Save briglx/fefb69e4c3f440ca169bb89d85422186 to your computer and use it in GitHub Desktop.
Get VM CPU for the past week
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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