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.
looks gr8 ..
can i get the same for max/min as well ?
lovely, BTW Don you can sort that on excel ;)
awesome! One question though,
> If I put ‘cpu.usage.average’ instead of ‘cpu.usagemhz.average’ will the output be in ‘%’?
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
Thanks.. How Can I do it for a particular cluster?
Prefix it with
Get-Cluster clustername | Get-VM | Where {$_.PowerState -eq “PoweredOn”} | Select Name, Host, NumCpu, MemoryMB
What time frame is the average % based on for this command?
Hi, very nice report, can I get CPU utilization for past 1 hour
Yep, change the (Get-Date).AddDays(-30) to (Get-Date).AddHours(-1) and you are set.
This post saved me so much time, thank you!
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?
Thank you Cedric.
CHANGE:
FROM: Get-VM | Where {$_.PowerState -eq “PoweredOn”} | Select Name ….
TO: Get-VM -Name XXXX | Select Name …..
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
wonderful script
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.
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
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