PowerCLi – Compare vm cpu MHz peak with limit

So this is a bit of a rare one, the script will run through a cluster and collect the single highest vm cpu mhz value for that day, as well as cpu limit for each vm – this will make sense if you use cpu limits on our vms – yeap, I know it’s not recommended…*

yellow bricks – limiting your vCPU

Feel free to copy and comment out the lines referring to $vmCpuLimit
From excel I use a simple formula to compare values [ =cell cpu mhz peak>(cell cpu limit*95%)] to flag which machines have touched the limit during the previous day


[code language=”powershell”]
# Get-VmCpuMHzPeak.ps1
# russ oct 2015
# Script reports the maximum used CPU of each VM during the specified period
# acknowledgement Jason Coleman – http://virtuallyjason.blogger.com

# Set file path, filename, date and time
# This is my standard path, you should adjust as needed
$filepath = "C:\vSpherePowerCLI\Output\"
$filename = "vmCpuPeak"
$initalTime = Get-Date
$date = Get-Date ($initalTime) -uformat %Y%m%d
$time = Get-Date ($initalTime) -uformat %H%M

Write-Host "———————————————————" -ForegroundColor DarkYellow
Write-Host "Output will be saved to:" -ForegroundColor Yellow
Write-Host $filepath$filename-$date$time".csv" -ForegroundColor White
Write-Host "———————————————————" -ForegroundColor DarkYellow

# Get Cluster info and populate $vms
$Cluster = Get-Cluster
$countCL = 0
Write-Output " "
Write-Output "Clusters: "
Write-Output " "
foreach($oC in $Cluster){
Write-Output "[$countCL] $oc"
$countCL = $countCL+1
$choice = Read-Host "Which Cluster do you want to review?"
Write-Output " "
Write-host "please wait for script to finish, it may take a while…." -ForegroundColor Yellow
$cluster = get-cluster $cluster[$choice]
$vms = Get-cluster $cluster | Get-VM

# Set Metric and start point (-1 day)
$metric = "cpu.usagemhz.average"
$metric2 = "cpu.ready.summation"
$Days = 1
$start = (Get-Date).AddDays(-$Days)

# Create empty results array to hold values
$resultsarray =@()

# Outer loop – move through vms, resets $MaxActive and $DateOfInterest to 0
foreach ($vm in $vms)
$MaxActive = 0
$DateOfInterest = 0
$NumCpus = 0

# Inner loop1 – Collects stat values of $metric for single vm
foreach ($Report in $(Get-Stat -Entity $vm -Stat $metric -Start $start -MaxSamples 1000 -IntervalMins 1 -Erroraction "silentlycontinue"))
# if loop – Compute max stat value of $metric for single vm
if ($Report.Value -gt $MaxActive)
$MaxActive = $Report.Value
$DateOfInterest = $Report.Timestamp

# Collect number of vCpus
$NumCpus = $vm.NumCpu
# Collect Cpu Limit
$vmCpuLimit = $vm.VMResourceConfiguration.CpuLimitMhz


# Create an array object to hold results, and add data as attributes using the add-member commandlet
$resultObject = new-object PSObject
$resultObject | add-member -membertype NoteProperty -name "VM Name" -Value $vm
$resultObject | add-member -membertype NoteProperty -name "Date of Entry" -Value $DateOfInterest
$resultObject | add-member -membertype NoteProperty -name "cpu mhz peak" -Value $MaxActive
$resultObject | add-member -membertype NoteProperty -name "num cpus" -Value $NumCpus
$resultObject | add-member -membertype NoteProperty -name "cpu mhz Limit" -Value $vmCpuLimit

# Write array output to results
$resultsarray += $resultObject

# output to gridview
$resultsarray | Out-GridView

# export to csv
$resultsarray | Export-Csv $filepath$filename"-"$date$time".csv" -NoType


Leave a comment

Your email address will not be published. Required fields are marked *

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