Which host should i deploy to based off current CPU & Memory usage?

The function “VP-WhichHost” will check the average CPU & Memory utilization of a specified cluster and then display all hosts that are below the average. All results are taken from real time metrics. It is broken out into 3 sections of “Below Average Memory”, “Below Average CPU” & “Below both AVG CPU & Memory” to allow you to pick the most appropriate host for your VM deployment.
Im using my usual variables:
$Global:DCChoice – DC Choice
$Global:CLUChoice – Cluster Choice

function VP-WhichHost {
$whesx = Get-datacenter $global:DCChoice | Get-Cluster $global:ClusterChoice | Get-VMHost
$memstats = Get-Stat -Entity $whesx -Stat "mem.usage.average" -Realtime -MaxSamples 1
$memavg = $memstats | Measure-Object -Property Value -Average | Select -ExpandProperty Average
$cpustats = Get-Stat -Entity $whesx -Realtime -MaxSamples 1 | Where {$_.MetricId -eq "cpu.usage.average" -and $_.Instance -eq ""}
$cpuavg = $cpustats | Measure-Object -Property Value -Average | Select -ExpandProperty Average
$BelAvgMemStats = $memstats | where{$_.Value -lt $memavg} | Select -ExpandProperty Entity
$BelAvgCPUStats = $cpustats | where{$_.Value -lt $cpuavg} | Select -ExpandProperty Entity
Write-Host " ================ Which host to deploy to in $global:ClusterChoice ================" -ForegroundColor green
Write-Host "Below Average CPU" -ForegroundColor green
Write-Host "All Below Average" -ForegroundColor green
#$BelAvgMemStats | where {$_.Name -Match $BelAvgCPUStats.Name}
(Compare-Object -ReferenceObject $BelAvgMemStats -DifferenceObject $BelAvgCPUStats -ExcludeDifferent -IncludeEqual).InputObject.Name
Write-Host -NoNewLine "Press any key to continue…";
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')


Leave a Reply

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.