PowerCLI: how to get unreserved CPU and Memory resource for each ESXi host?

I had reservation for some VMs in clusters, I wanted to find out how much free (unreserved) resources are

exist on each esxi host, it could help us regarding VMs migration.

1. Connect to the vCenter Server:

Connect-VIServer VC_ADDRESS

2. Then for each host in our cluster, we get total resource and also for each powered on VM, we get the reserved CPU and Memory and calculate sum of them, at the end we can get result by doing math:

foreach ($esx in (Get-Cluster CLUSTER_NAME | get-vmhost))
{
Write-Host $esx
$CPUCap = Get-VMHost $esx | Select @{N='Total CPU Capacity MHz';E={[math]::Round($_.CpuTotalMhz)}}
$CPURes = Get-VMHost $esx | get-vm | Where-Object {$_.PowerState -eq "PoweredOn"} | Select @{N="Total CPU Reservation MHz";E={$_.ExtensionData.ResourceConfig.CpuAllocation.Reservation}} |
Measure-Object -Property "Total CPU Reservation MHz" -Sum
Write-Host = "Total CPU Capacity MHz" = $($CPUCap.'Total CPU Capacity MHz') -ForegroundColor Cyan
Write-Host = "Total CPU Reservation MHz" = $($CPURes.Sum) -ForegroundColor Yellow
Write-Host = "Total CPU Free MHz" = ($CPUCap.'Total CPU Capacity MHz' - $CPURes.Sum) -ForegroundColor Green
$MemCap = Get-VMHost $esx | Select @{N='Total Memory Capacity MB';E={[math]::Round($_.MemoryTotalMB)}}
$MemRes = Get-VMHost $esx | get-vm | Where-Object {$_.PowerState -eq "PoweredOn"} | Select @{N="Total Memory Reservation MB";E={$_.ExtensionData.ResourceConfig.MemoryAllocation.Reservation }} |
Measure-Object -Property "Total Memory Reservation MB" -Sum
Write-Host = "Total Memory Capacity MB" = $($MemCap.'Total Memory Capacity MB') -ForegroundColor Cyan
Write-Host = "Total Memory Reservation MB" = $($MemRes.Sum) -ForegroundColor Yellow
Write-Host = "Total Memory Free MB" = ($MemCap.'Total Memory Capacity MB' - $MemRes.Sum) -ForegroundColor Green
}

About admin

Check Also

VMware Telco Cloud Automation – Part04

VMware Telco Cloud Manager and Control Plane: VMware Telco Cloud Automation onboards and orchestrates workloads …

Leave a Reply

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