Visitors

PowerCLI: Average CPU, Memory, Network and Disk usage

This simple PowerShell/PowerCLI script calculates the average CPU, Memory, Network and Disk usage for powered on virtual machines over the last 30 days, 5 minutes interval.

Amend ‘AddDays(-N)‘ and ‘-IntervalMins 5‘ if needed.

Get-VM | Where {$_.PowerState -eq "PoweredOn"} | Select Name, Host, NumCpu, MemoryMB, `
@{N="CPU Usage (Average), Mhz" ; E={[Math]::Round((($_ | Get-Stat -Stat cpu.usagemhz.average -Start (Get-Date).AddDays(-30) -IntervalMins 5 | Measure-Object Value -Average).Average),2)}}, `
@{N="Memory Usage (Average), %" ; E={[Math]::Round((($_ | Get-Stat -Stat mem.usage.average -Start (Get-Date).AddDays(-30) -IntervalMins 5 | Measure-Object Value -Average).Average),2)}} , `
@{N="Network Usage (Average), KBps" ; E={[Math]::Round((($_ | Get-Stat -Stat net.usage.average -Start (Get-Date).AddDays(-30) -IntervalMins 5 | Measure-Object Value -Average).Average),2)}} , `
@{N="Disk Usage (Average), KBps" ; E={[Math]::Round((($_ | Get-Stat -Stat disk.usage.average -Start (Get-Date).AddDays(-30) -IntervalMins 5 | Measure-Object Value -Average).Average),2)}} |`
Export-Csv -Path d:AverageUsage.csv

Example of the script output:

Name Host NumCpu MemoryMB CPU Usage (Average), Mhz Memory Usage (Average), % Network Usage (Average), KBps Disk Usage (Average), KBps
dsva02 esxc02n02.vStrong.local 2 2048 61.91 5.63 28.7 17
VSR001 esxc02n01.vStrong.local 2 4096 63.66 9.7 6.86 15.11
SQL100 esxc02n02.vStrong.local 2 8192 40.64 2.67 0.07 3.75
VCS001 esxc02n03.vStrong.local 2 8192 177.84 5.59 0.14 15.53
VCS002 esxc02n02.vStrong.local 2 8192 36.47 1.85 0.09 6.14

I hope you find this useful.

20 comments to PowerCLI: Average CPU, Memory, Network and Disk usage

  • Don

    can i get the same for max/min as well ?

  • lovely, BTW Don you can sort that on excel ;)

  • Sho

    awesome! One question though,

    > If I put ‘cpu.usage.average’ instead of ‘cpu.usagemhz.average’ will the output be in ‘%’?

  • Anthony Leanna

    For some reason I am only getting 0 in response to my CPU usage. I am only using that section of the script. Any ideas ?

  • new to scripting can you give me a example I tired running this with run power shell

  • Bando

    Thanks.. How Can I do it for a particular cluster?

  • Tim washington

    What time frame is the average % based on for this command?

  • Shivakumar

    Hi, very nice report, can I get CPU utilization for past 1 hour

  • Oliver

    This post saved me so much time, thank you!

  • Leonard

    What exactly are we reading on the Disk Usage Average? When I view it in VCenter, I do not find the same value from the script.

  • Excellent script, how would i change it to report on specific VMs?

    • Mark Strong

      Thank you Cedric.
      CHANGE:
      FROM: Get-VM | Where {$_.PowerState -eq “PoweredOn”} | Select Name ….
      TO: Get-VM -Name XXXX | Select Name …..

  • Andrew

    When I run this I only get back 1 week of data. I have tried to adjust the intervalmins but doesnt seem to go back further. Any idea why?
    get-vm SERVERNAME| get-stat -Cpu -IntervalMins 30 -Start (Get-Date).AddDays(-30) -MaxSamples 99999 |
    Where{$_.metricid -eq “cpu.usage.average”} | Select Timestamp, value | Export-Excel -Path C:\Temp\SERVERNAME.xlsx -WorkSheetname SERVERNAME

  • VENKAT RAM REDDY REDDY ALGUBELLI

    wonderful script

  • Stefan

    Good Afternoon… great website and first time viewer.. have been tasked to provide some scripts for cluster/hosts data gathering and seeing as i have never produced a script, was wondering if someone could to 5 mins to get me going?..looking to gather the following information from my vcenter environment:

    Cluster
    Min/Average/Max CPU usage – Daily/Weekly report
    Min/Average/Max Memory – Daily/Weekly report
    Ratio of Physical/Virtual CPU
    Ratio of Physical /Virtual Memory

    Hosts
    Min/Average/Max CPU – Daily/Weekly
    Min/Average/Max Memory – Daily/Weekly

    Housekeeping
    List of Snapshots
    Datastore Usage

    any help greatly appreciated.

  • NA

    For me it seems this would give i day average due to vCenter DB rollup interval,

    For one month average you probably use “(Get-Date).AddDays(-30) -IntervalMins 120”

    vCenter DB rolled Historical Interval:

    5 min interval, 1 day retention
    30 min interval, 1 week retention
    2 hr interval, 1 month retention
    1 day interval, 1 year retention

  • Arun

    Finally some straight forward script to use.
    Thank you very much.
    I’m looking to export data of 60 days. Could you please suggest using the values for AddDays and IntervalMins?
    Also I’m getting all ‘0’s for Disk information.

    Thanks,
    Arun

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

  

  

  

This site uses Akismet to reduce spam. Learn how your comment data is processed.