Close Menu
PowerShell.roPowerShell.ro
  • Home
  • Automation
  • Powershell
  • VMWare
  • Windows
LinkedIn X (Twitter) Facebook
PowerShell.roPowerShell.ro
  • Home
  • Automation
  • Powershell
  • VMWare
  • Windows
LinkedIn Facebook X (Twitter)
PowerShell.roPowerShell.ro
Home»Automation»PowerCLI script to check Cluster Overcommitment ratio
Automation

PowerCLI script to check Cluster Overcommitment ratio

Catalin CristescuBy Catalin CristescuDecember 6, 2023
Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
Share
Facebook Twitter LinkedIn Pinterest Email

Efficient resource utilization is a key factor in maintaining a healthy VMware vSphere environment. As virtualization environments grow, it becomes crucial to monitor and optimize resource allocation to prevent overcommitment, ensuring the smooth operation of virtual machines. In this blog post, we’ll explore a PowerCLI script designed to check the Cluster Overcommitment Ratio, providing valuable insights into the CPU and RAM usage across your vSphere clusters.

Understanding Cluster Overcommitment:

Cluster Overcommitment is a metric that reflects how efficiently your cluster utilizes its available resources, specifically focusing on CPU and RAM. By calculating the ratio of powered-on virtual CPUs (vCPUs) or RAM to the total physical resources, administrators can identify potential bottlenecks and optimize their infrastructure.

The PowerCLI Script:

The script is designed to connect to a vCenter server, iterate through each cluster, and generate a detailed report containing key metrics.

param(
    [String]$vcenter,
    [String]$userName,
    [String]$password
)

# Import required modules
Import-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue
Import-Module -Name "C:\temp\importexcel.7.8.4\importexcel.psd1" -ErrorAction SilentlyContinue

# Set PowerCLI configuration
Set-PowerCLIConfiguration -ProxyPolicy NoProxy -DefaultVIServerMode multiple -Scope User -InvalidCertificateAction ignore -Confirm:$false | Out-Null

# Connect to vCenter
try {
    Connect-VIServer -Server $vcenter -User $userName -Password $password -ErrorAction Stop | Out-Null
} catch [VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.InvalidLogin] {
    Write-Host "Cannot connect to $vcenter with provided credentials" -ForegroundColor Red
    return
} catch [VMware.VimAutomation.Sdk.Types.V1.ErrorHandling.VimException.ViServerConnectionException] {
    Write-Host "Cannot connect to $vcenter - check IP/FQDN" -ForegroundColor Red
    return
} catch {
    Write-Host "Cannot connect to $vcenter - Unknown error" -ForegroundColor Red
    return
}

# Get all the clusters
$clusters = Get-Cluster
$data = @()

# Loop through the clusters and extract the required data
foreach ($cluster in $clusters) {
    $vmhosts = Get-VMHost -Location $cluster
    $num_esxis = $vmhosts.Count
    
    # Calculate cluster metrics
    $ClusterPoweredOnvCPUs = (Get-VM -Location $cluster | Where-Object {$_.PowerState -eq "PoweredOn" }).NumCpu | Measure-Object -Sum
    $ClusterCPUCores = ($vmhosts.NumCpu | Measure-Object -Sum).Sum

    $ClusterPoweredOnvRAM = (Get-VM -Location $cluster | Where-Object {$_.PowerState -eq "PoweredOn" }).MemoryGB | Measure-Object -Sum
    $ClusterPhysRAM = ($vmhosts.MemoryTotalGB | Measure-Object -Sum).Sum

    $oneesxi = if ($num_esxis) { $num_esxis - 1 } else { $null }
    $coresPerESXi = if ($num_esxis) { $ClusterCPUCores / $num_esxis } else { $null }
    $coresPerClusterMinusOne = $coresPerESXi * $oneesxi
    $TotalCoresPerClusterMinusOne = [Math]::Round($coresPerClusterMinusOne)

    $ramPerESXi = $ClusterPhysRAM / $num_esxis
    $ramPerClusterMinusOne = $ramPerESXi * $oneesxi
    $TotalRAMPerClusterMinusOne = [Math]::Round($ramPerClusterMinusOne)

    # Create an ordered hashtable for each cluster
    $property = [ordered]@{
        "vCenter" = $vcenter
        "Cluster Name" = $cluster.Name
        "Number of ESXi servers" = $num_esxis
        "pCPU" = $ClusterCPUCores
        "vCPU" = (Get-VM -Location $cluster | Measure-Object NumCpu -Sum).Sum
        "PoweredOn vCPUs" = $ClusterPoweredOnvCPUs.Sum
        "vCPU:pCPU Ratio" = [Math]::Round($ClusterPoweredOnvCPUs.Sum / $ClusterCPUCores, 3)
        "vCPU:pCPU Ratio with one ESXi failed" = [Math]::Round($ClusterPoweredOnvCPUs.Sum / $TotalCoresPerClusterMinusOne, 3)
        "CPU Overcommit (%)" = [Math]::Round(100 * ($ClusterPoweredOnvCPUs.Sum / $ClusterCPUCores), 3)
        'pRAM(GB)' = $ClusterPhysRAM
        'vRAM(GB)' = [Math]::Round((Get-VM -Location $cluster | Measure-Object MemoryGB -Sum).Sum, 2)
        'PoweredOn vRAM (GB)' = $ClusterPoweredOnvRAM.Sum
        'vRAM:pRAM Ratio' = [Math]::Round($ClusterPoweredOnvRAM.Sum / $ClusterPhysRAM, 3)
        "vRAM:pRAM Ratio with one ESXi failed" = [Math]::Round($ClusterPoweredOnvRAM.Sum / $TotalRAMPerClusterMinusOne, 3)
        'RAM Overcommit (%)' = [Math]::Round(100 * ($ClusterPoweredOnvRAM.Sum / $ClusterPhysRAM), 2)
    }

    # Add the hashtable to the data array
    $data += New-Object -TypeName psobject -Property $property
}

# Export the data to an Excel file
$filePath = "C:\temp\$($vcenter).xlsx"
$data | Export-Excel -Path $filePath -AutoSize -TableName "Data"

# Disconnect from vCenter
Disconnect-VIserver -Server $vcenter -Confirm:$false

Feel free to customize this script based on your specific reporting requirements and share your experiences or improvements in the comments below!

It should be noted that the script is not entirely authored by me; certain information and code snippets are sourced from various places, including blogs, GitHub repositories, and community forums.

cpu ratio overcommit pCPU powercli vCPU vexpert
Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
Previous ArticleSimplify Your Day-to-Day Tasks with These 5 Essential PowerShell Scripts
Next Article Update HPE ILO firmware using PowerShell
Catalin Cristescu
  • Website

Related Posts

Automation

PowerShell script for automated remediation for CrowdStrike issue

July 20, 2024
VMWare

vExperts stats 2024

May 27, 2024
Automation

Tagging Virtual Machines with the CISTag Module in PowerShell

February 1, 2024

Comments are closed.

Recent Posts
  • PowerShell script for automated remediation for CrowdStrike issue
  • vExperts stats 2024
  • Tagging Virtual Machines with the CISTag Module in PowerShell
  • Update HPE ILO firmware using PowerShell
  • PowerCLI script to check Cluster Overcommitment ratio
Categories
  • Automation (11)
  • Powershell (14)
  • Uncategorized (1)
  • VMWare (15)
  • Windows (2)
About Powershell.ro

Powershell.ro, the ultimate hub for tech enthusiasts! Dive into expert-written articles on PowerShell scripts, VMware, automation, Microsoft technologies, and DevOps practices. Unlock tips, tutorials, and solutions designed to empower IT professionals and boost your skills in automation and infrastructure management.

Recent Posts
  • PowerShell script for automated remediation for CrowdStrike issue
  • vExperts stats 2024
  • Tagging Virtual Machines with the CISTag Module in PowerShell
  • Update HPE ILO firmware using PowerShell
  • PowerCLI script to check Cluster Overcommitment ratio
LinkedIn
© 2025 PowerShell.ro.

Type above and press Enter to search. Press Esc to cancel.