PowerCLI – Get-VmMhzUsage

This will collects highest 10 samples of vm cpu usage within a set period

It is part of a series for investigating cpu constrained vms and can be run on either a single vm or a cluster

 

[code language=”powershell”]
# Acknowledgement: the absolutely brilliant Jason Coleman – http://virtuallyjason.blogspot.com.es/
# Modified russ 25/01/2016

# To identify vm cpu usage in mhz

# Collects highest 10 samples of vm cpu usage above within a set period
# Samples can be increased by adding -Samples <number of samples>
# Set period is determined by the vCenter statistics ‘5 minute’ ‘save for’ settings

#########################################################################################################
# Usage
# ie for a single vm:
# ./Get-VmMhzUsage.ps1 -VM <Virtual Machine Name> -Sample <number of samples to collect>
#
# ie for cluster
# ./Get-VmMhzUsage.ps1.ps1
#########################################################################################################

# Set variables
[CmdletBinding()]
param (
[string]$VM = " ",
[string]$Sample = " ",
[int]$Days = 3,
[string]$metric = "cpu.usagemhz.average"
)

# Set file path, filename, date and time
# This is my standard path, you should adjust as needed
$filepath = "C:\vSpherePowerCLI\Output\"
$filename = "vmMhzUsage"
$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

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

# Set samples value
if ($Sample -eq " "){
$Samples = 10
}
else{
$Samples = $Sample
}

# Option to run script against a single vm or specific cluster
# Remove the Get Cluster section and replace with $vms = Get-VM to run on all vms in vcenter
if ($vm -eq " "){
# 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]

#Filter only powered on vms
$vms = Get-Cluster $Cluster | Get-VM | where {$_.powerstate -eq "PoweredOn"}

}

else{
$vms = Get-VM -name $vm
}

Write-Host "———————————————————" -ForegroundColor DarkYellow

Write-Host "Collecting Statistics for the following vms:" -ForegroundColor Yellow

Write-Host "———————————————————" -ForegroundColor DarkYellow

$vms
Write-Host "———————————————————" -ForegroundColor DarkYellow

# Set start time
$start = (Get-Date).AddDays(-$Days)

# Outer loop – iterates each vm in the $vms variable
foreach ($vm in $vms){

#
# Inner loop – runs through the statistics and collects the highest 25 cpu usage in mhz values for each vm
foreach ($Report in $(Get-Stat -Entity $vm -IntervalSecs 300 -Stat $metric -Start $start
´| sort-object value -descending | Select-Object -first $Samples -Erroraction "silentlycontinue")){

write-output "$($Report.Entity), $($Report.Timestamp),$($Report.Value)"

# 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 "entity" -Value $Report.Entity
$resultObject | add-member -membertype NoteProperty -name "timestamp" -Value $Report.Timestamp
$resultObject | add-member -membertype NoteProperty -name "cpu usage mhz" -Value $Report.Value

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

}
}

# output to gridview
$resultsarray | Out-GridView

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

[/code]