Thecompany have 14hosts and 1500VMs
I decided tochallengethe appointmentof limitsto the resourcepools
The scriptcollects information aboutresourcepools, powered On virtual machine, the total number ofvСPUinto a resourcepooland designatedlimits.
Tocorrect destinationlimitsderived a formula LimitVCPU = (155+420 / [math]::pow($TotVCPU, 0.5 ))*$TotVCPU), ofwhichare considered to belimits. The sum of theselimitswill not exceed90% ofthe CPUof the host, it will beto control thesupply ofthe host.
Name | TotalVCPU | LimitVCPU | ESXHost | CpuTotalMhz | SummLimitCPU | Coefficient | NewLimitVCPU |
RP1 | 0 | 0 | srv-esx-01 | 19192 | 8619 | 2,004 | 200 |
RP2 | 1 | 575 | 1152 | ||||
RP3 | 2 | 904 | 1812 | ||||
RP4 | 2 | 904 | 1812 | ||||
RP5 | 4 | 1460 | 2926 | ||||
RP5 | 19 | 4776 | 9571 |
cls
$watch = [System.Diagnostics.Stopwatch]::StartNew()
$watch.Start() #Start time
$myCol = @()
$ESXHosts = Get-VMHost
$TotVCPU = 0
$LimitVCPU = 0
$CpuTotalMhz = 0
$ii = 0
foreach ($ESXHost in $ESXHosts){
$SummLimitCPU = 0
$RPs = Get-ResourcePool -Location $ESXHost | ?{$_.Name -ne "Resources"}
$i = 0
foreach($RP in $RPs){
$VMs = Get-VM -Location $RP | ?{$_.PowerState -eq "PoweredOn"}
$TotVCPU = (($VMs | ?{$_.PowerState -eq "PoweredOn"} | select NumCPU | Measure-Object -Property NumCpu -Sum).Sum/1)
if ($TotVCPU -ne 0)
{$LimitVCPU = [Math]::Round((155+420 / [math]::pow($TotVCPU, 0.5 ))*$TotVCPU)}
else {$LimitVCPU = 0}
if ($LimitVCPU.GetType().FullName -eq "System.Int32")
{$SummLimitCPU = $SummLimitCPU + $LimitVCPU}
if ($RP -eq $RPs[-1])
{$CpuTotalMhz = Get-VMhost -Name $ESXHost | select -ExpandProperty CpuTotalMhz
if ($SummLimitCPU -ne 0){
foreach($RP in $RPs){$Coeffecient=[Math]::Round(($CpuTotalMhz / $SummLimitCPU)*0.9,3)}}}
else
{$CpuTotalMhz = 0; $Coeffecient=1}
$Details = "" | Select Name,VMs,TotalVCPU,LimitVCPU,ESXHost,CpuTotalMhz,SummLimitCPU,Coefficient,NewLimitVCPU
$Details.Name = $RP.Name
#$Details.VMs = $VMs.Name | Out-String
$Details.TotalVCPU = $TotVCPU
$Details.LimitVCPU = $LimitVCPU
$Details.ESXHost = $ESXHost
$Details.CpuTotalMhz = $CpuTotalMhz
$Details.SummLimitCPU = $SummLimitCPU
$Details.Coefficient = $Coeffecient
$Details.NewLimitVCPU = [Math]::Round($Coeffecient * $LimitVCPU)
#replace Coefficient, SummLimitCPU, NewLimitVCPU in array foreach RP
$myCol += $Details
$i = $i+1
}
if ($ii -eq 0) {$ii = $ii+($i-1)}
else {$ii = $ii+$i}
$point = $ii
for (;$i -gt 1; $i--)
{
$point = $point - 1
$myCol[$point].Coefficient = $myCol[$ii].Coefficient
$myCol[$point].SummLimitCPU = $myCol[$ii].SummLimitCPU
if ($myCol[$point].TotalVCPU -eq 0) {$myCol[$point].NewLimitVCPU = 200}
else {$myCol[$point].NewLimitVCPU = [Math]::Round($myCol[$ii].Coefficient * $myCol[$point].LimitVCPU)}
# set New CPULimit foreach RP
Set-ResourcePool -ResourcePool $myCol[$point].Name -CpuLimitMhz $myCol[$point].NewLimitVCPU
}
}
$myCol | Export-Csv -Delimiter ";" -Path C:\PowerCLI\Report\ResourcePool.csv
$watch.Stop() #Stop time
Write-Host $watch.Elapsed #Run time
I will be gladto listen tocomments andsuggestions for improvingthe script