how can I get all my VM CPU utilization witch is present in one resource group

Gazanfar Syed 0 Reputation points
2023-12-28T14:53:44.6766667+00:00

To monitor and retrieve CPU utilization for all your Virtual Machines (VMs) in a specific Azure Resource Group, you can use Azure PowerShell. Please guide us step-by-step.

Azure Monitor
Azure Monitor
An Azure service that is used to collect, analyze, and act on telemetry data from Azure and on-premises environments.
3,516 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Dillon Silzer 57,686 Reputation points
    2023-12-29T05:19:22.9733333+00:00

    Hello Gazanfar,

    You can use the following PS script cited from here by vipullag-MSFT:

    #get all vms in a subscription
    $vms = Get-AzVM -ResourceGroupName "ResourceGroupName"
    
     #get the last 3 days data
     #end date
     $et=Get-Date
    
     #start date
     $st=$et.AddDays(-3)
    
      $arr =@()
    
     foreach($vm in $vms)
     {
     
     $s = ""
     
     #percentage cpu usage
    $cpu = Get-AzMetric -ResourceId $vm.Id -MetricName "Percentage CPU" -DetailedOutput -StartTime $st `
     -EndTime $et -TimeGrain 12:00:00  -WarningAction SilentlyContinue
    
    
     
     # 3 days == 72hours == 12*6hours
    
     $cpu_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
     }
    
    
     # add all the related info to the string
     $s = "VM Name: " + $vm.name + "; Resource Group: " + $vm.ResourceGroupName + "; CPU: " +$cpu_total
    
     # add the above string to an array
     $arr += $s
     }
    
     #check the values in the array
     $arr
    

    Hopefully this helps you. You may need to edit some variables, but this grabs for all VMs in subscription's resource group. (this checks the last 3 days).

    I have made adjustments to the code to include specifically for a Resource Group.


    If this is helpful please accept answer.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.