PowerCLI – Get-VmReadySpikesForXDays

Check up on ready values per cluster or vm

PowerCLI Script for determining virtual machine ready values over a set threshold during a specified period. Use this script before applying vm cpu ready alarms to vCenter

Read the explanatory post, so as to understand what is being generated and why it is important.
Note: this is a very cool piece of code, it’s simple and incredibly relevant for performance troubleshooting

for a single vm:  ./Get-VmReadySpikesForXDays.ps1 -VM<virtual machine name>
for a cluster  ./Get-VmReadySpikesForXDays.ps1 and select cluster


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

# This script identifies any VM that has CPU Ready metrics above a threshold within a set period.
# Single vm or all vms in vCenter can be checked
# ie for a single vm: ./Vm-Ready.ps1 -VM &lt;Virtual Machine Name&gt;

# Multiple outputs are generated if the vm passed the threshold value multiple times
# It reports when each VM experienced the load

# Set value for [Ready] Threshold (only values above this are collected) – set at 9.9%
# Set value to 3 days – this take into account vCenter stats roll up
# vCenter &gt; administration &gt; statistics Interval Duration 5 Minutes = 3 Days
# Statistics level = 3

# Set variables
param (
[string]$VM = " ",
[int]$Days = 3,
[int]$Threshold = 9.9,
[string]$metric = "cpu.ready.summation"

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

# 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]
$vms = Get-cluster $cluster | Get-VM

$vms = Get-VM -name $VM

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

# Outer loop – iterates $vms
foreach ($vm in $vms)

# Inner loop – collects stat values of $metric for each vm in Svms
foreach ($Report in $(Get-Stat -Entity $vm -IntervalSecs 300 -Stat $metric -Start $start -Erroraction "silentlycontinue")){
$ReadyPercentage = (($Report.Value/10)/$Report.IntervalSecs)
if ($ReadyPercentage -gt $Threshold){
$PerReadable = "$ReadyPercentage".substring(0,4)
write-output "$($Report.Entity), $($Report.Timestamp), $PerReadable%"

# 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.ready.summation" -Value $PerReadable%

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



# output to gridview
$resultsarray | Out-GridView

# export to csv
# $resultsarray| Export-csv C:\vSpherePowerCLI\Output\vmCpuPeak.csv -notypeinformation
$resultsarray | Export-Csv $filepath$filename"-"$date$time".csv" -NoType