Skip to content

VirtuallyThatGuy

Anything VMware , PowerCLI, PowerShell, Automation and some Windows

Menu
  • Home
  • PowerCLI
  • VMware
  • Automation
  • Windows
  • About
Menu

How to Calculate pCPU to vCPU and pMem to vMem Ratio Using PowerCLI – VirtuallyThatGuy

Posted on 25 April 20186 December 2022 by VirtuallyThatGuy

In my time working for an Online betting company, I use to do a lot of capacity planning and cluster over commit analysis for our IT director. This sphered me on to try and automate the process…Thanks to the vmware powercli you can automate the process with below. So once again welcome to VirtuallyThatGuy – this is a quick script to help you determine your cluster compute resource and utilisation using the below script. This one is for the cluster capacity planners. VMTurbo or Turbonomics is a better alternative If you love fancy graphs.

 

# 

$outputFile​​ =​​ "C:\scripts\CPU-Memory-Ratio.csv"

$VC​​ =​​ "uk3p-vc01.lab.local","ntcp-vc01.lab.local"  #Vcenter Details Here

##Connect to the vCenter

Connect-VIServer​​ $VC​​ #-User​​ "rboadi@lab.local" -Password "xxxxxxx"

$Output​​ =@()

Get-Cluster​​ |​​ %{

$hypCluster​​ =​​ $_

## get the GenericMeasureInfo for the desired properties for this cluster's hosts

$infoCPUMEM​​ =​​ Get-View​​ -ViewType​​ HostSystem​​ -Property​​ Hardware.CpuInfo,Hardware.memorysize​​ -SearchRoot​​ $hypCluster.Id​​ |

Select​​ @{n="NumCpuSockets"; e={$_.Hardware.CpuInfo.NumCpuPackages}},​​ @{n="NumCpuCores"; e={$_.Hardware.CpuInfo.NumCpuCores}},​​ @{n="NumCpuThreads"; e={$_.Hardware.CpuInfo.NumCpuThreads}},@{n="PhysicalMem"; E={""+[math]::round($_.Hardware.MemorySize​​ /​​ 1GB,​​ 0)}}​​ |

Measure-Object​​ -Sum​​ NumCpuSockets,NumCpuCores,NumCpuThreads,PhysicalMem

## return an object with info about VMHosts' CPU characteristics

$temp=​​ New-Object​​ psobject

$datacenter​​ =​​ Get-Datacenter​​ -Cluster​​ $hypCluster.Name

$NumVMHosts​​ =​​ if​​ ($infoCPUMEM) {$infoCPUMEM[0].Count}​​ else​​ {0}

$NumCpuSockets​​ =​​ ($infoCPUMEM​​ |​​ ?{$_.Property​​ -eq​​ "NumCpuSockets"}).Sum

$NumCpuCores​​ =​​ ($infoCPUMEM​​ |​​ ?{$_.Property​​ -eq​​ "NumCpuCores"}).Sum

$vmdetails​​ =​​ Get-VM​​ -Location​​ $hypCluster

$NumvCPU​​ =​​ (​​ $vmdetails​​ |​​ Measure-Object​​ NumCpu​​ -Sum).Sum

$VirtualMem=​​ [Math]::Round(($vmdetails​​ |​​ Measure-Object​​ MemoryGB​​ -Sum).Sum,​​ 2)

$PhysicalMem​​ =​​ ($infoCPUMEM​​ |​​ ?{$_.Property​​ -eq​​ "PhysicalMem"}).Sum

##Calculating the vCPU to pCPU ratio​​ AND vRAM to pRAM ratio.

if​​ ($NumvCPU​​ -ne​​ "0") {$cpuRatio=​​ "$("{0:N2}"​​ -f​​ ($NumvCPU/$NumCpuCores))"​​ +​​ ":1"}

if​​ ($VirtualMem​​ -ne​​ "0") {$memRatio=​​ "$("{0:N2}"​​ -f​​ ($VirtualMem/$PhysicalMem))"​​ +​​ ":1"}

$temp​​ |​​ Add-Member​​ -MemberType​​ Noteproperty​​ "Datacenter"​​ -Value​​ $datacenter

$temp​​ |​​ Add-Member​​ -MemberType​​ Noteproperty​​ "ClusterName"​​ -Value​​ $hypCluster.Name

$temp​​ |​​ Add-Member​​ -MemberType​​ Noteproperty​​ "NumVMHosts"​​ -Value​​ $NumVMHosts

$temp​​ |​​ Add-Member​​ -MemberType​​ Noteproperty​​ "NumPCPUSockets"​​ -Value​​ $NumCpuSockets

$temp​​ |​​ Add-Member​​ -MemberType​​ Noteproperty​​ "NumPCPUCores"​​ -Value​​ $NumCpuCores

$temp​​ |​​ Add-Member​​ -MemberType​​ Noteproperty​​ "NumvCPU"​​ -Value​​ $NumvCPU

$temp​​ |​​ Add-Member​​ -MemberType​​ Noteproperty​​ "vCPU-pCPUCoreRatio"​​ -Value​​ $cpuRatio

$temp​​ |​​ Add-Member​​ -MemberType​​ Noteproperty​​ "PhysicalMem(GB)"​​ -Value​​ $PhysicalMem

$temp​​ |​​ Add-Member​​ -MemberType​​ Noteproperty​​ "VirtualMem(GB)"​​ -Value​​ $VirtualMem

$temp​​ |​​ Add-Member​​ -MemberType​​ Noteproperty​​ "vRAM-pRAMRatio"​​ -Value​​ $memRatio

$Output+=$temp

}

$Output​​ |​​ Sort-Object​​ Account​​ |​​ Export-Csv​​ -NoTypeInformation​​ $outputFile

 

 

 

 

 

 

 

Output like below

 

Datacenter

ClusterName

NumVMHosts

NumPCPUSockets

NumPCPUCores

NumvCPU

vCPU-pCPUCoreRatio

PhysicalMem(GB)

VirtualMem(GB)

vRAM-pRAMRatio

LON-Datacenter

DTCP-Cluster

3

6

12

8

0.67:1

48

15

0.31:1

LON-Datacenter

UK3P-Cluster

3

6

12

4

0.33:1

48

6

0.13:1

LON-MGMT-Datacenter

LON-MGMT-Cluster

2

4

8

14

1.75:1

46

41

0.89:1

USA-Datacenter

HDCP-Cluster

3

6

12

15

1.25:1

48

28.5

0.59:1

USA-Datacenter

NTCP-Cluster

3

6

12

20

1.67:1

48

56.7

1.18:1

USA-MGMT-Datacenter

USA-MGMT-Cluster

2

4

8

13

1.63:1

46

42

0.91:1

 

# 
$outputFile = "C:\scripts\CPU-Memory-Ratio.csv"
$VC = "uk3p-vc01.lab.local","ntcp-vc01.lab.local"  #Vcenter Details Here
##Connect to the vCenter
Connect-VIServer $VC #-User "rboadi@lab.local" -Password "xxxxxxx"
$Output =@()
Get-Cluster | %{
$hypCluster = $_
## get the GenericMeasureInfo for the desired properties for this cluster's hosts
$infoCPUMEM = Get-View -ViewType HostSystem -Property Hardware.CpuInfo,Hardware.memorysize -SearchRoot $hypCluster.Id |
Select @{n="NumCpuSockets"; e={$_.Hardware.CpuInfo.NumCpuPackages}}, @{n="NumCpuCores"; e={$_.Hardware.CpuInfo.NumCpuCores}}, @{n="NumCpuThreads"; e={$_.Hardware.CpuInfo.NumCpuThreads}},@{n="PhysicalMem"; E={""+[math]::round($_.Hardware.MemorySize / 1GB, 0)}} |
Measure-Object -Sum NumCpuSockets,NumCpuCores,NumCpuThreads,PhysicalMem
## return an object with info about VMHosts' CPU characteristics
$temp= New-Object psobject
$datacenter = Get-Datacenter -Cluster $hypCluster.Name
$NumVMHosts = if ($infoCPUMEM) {$infoCPUMEM[0].Count} else {0}
$NumCpuSockets = ($infoCPUMEM | ?{$_.Property -eq "NumCpuSockets"}).Sum
$NumCpuCores = ($infoCPUMEM | ?{$_.Property -eq "NumCpuCores"}).Sum
$vmdetails = Get-VM -Location $hypCluster
$NumvCPU = ( $vmdetails | Measure-Object NumCpu -Sum).Sum
$VirtualMem= [Math]::Round(($vmdetails | Measure-Object MemoryGB -Sum).Sum, 2)
$PhysicalMem = ($infoCPUMEM | ?{$_.Property -eq "PhysicalMem"}).Sum
##Calculating the vCPU to pCPU ratio AND vRAM to pRAM ratio.
if ($NumvCPU -ne "0") {$cpuRatio= "$("{0:N2}" -f ($NumvCPU/$NumCpuCores))" + ":1"}
if ($VirtualMem -ne "0") {$memRatio= "$("{0:N2}" -f ($VirtualMem/$PhysicalMem))" + ":1"}
$temp | Add-Member -MemberType Noteproperty "Datacenter" -Value $datacenter
$temp | Add-Member -MemberType Noteproperty "ClusterName" -Value $hypCluster.Name
$temp | Add-Member -MemberType Noteproperty "NumVMHosts" -Value $NumVMHosts
$temp | Add-Member -MemberType Noteproperty "NumPCPUSockets" -Value $NumCpuSockets
$temp | Add-Member -MemberType Noteproperty "NumPCPUCores" -Value $NumCpuCores
$temp | Add-Member -MemberType Noteproperty "NumvCPU" -Value $NumvCPU
$temp | Add-Member -MemberType Noteproperty "vCPU-pCPUCoreRatio" -Value $cpuRatio
$temp | Add-Member -MemberType Noteproperty "PhysicalMem(GB)" -Value $PhysicalMem
$temp | Add-Member -MemberType Noteproperty "VirtualMem(GB)" -Value $VirtualMem
$temp | Add-Member -MemberType Noteproperty "vRAM-pRAMRatio" -Value $memRatio
$Output+=$temp
}
$Output | Sort-Object Account | Export-Csv -NoTypeInformation $outputFile

3 thoughts on “How to Calculate pCPU to vCPU and pMem to vMem Ratio Using PowerCLI – VirtuallyThatGuy”

  1. Michael says:
    25 January 2019 at 07:08

    Hi may I know you’re formula for these?

    Reply
    1. VirtuallyThatGuy says:
      16 March 2019 at 23:20

      Hi Michael, please refer to the “##Calculating the vCPU to pCPU ratio​​ AND vRAM to pRAM ratio.” section of the script

      Reply
  2. pac1085 says:
    26 March 2021 at 12:16

    Helpful script, thanks! I changed it a bit for our environment, including limiting it to only powered on VM’s. (We have a large number of decommissioned VM’s that are powered off but can’t be deleted for various reasons, and I didn’t want them included in capacity metrics since they won’t be used again)

    I modified this line:
    $vmdetails​​ =​​ Get-VM​​ -Location​​ $hypCluster
    to
    $vmdetails​​ =​​ Get-VM​​ -Location​​ $hypCluster | where-object “PowerState” -eq “PoweredOn”

    and it worked well.

    Reply

Leave a Reply Cancel reply

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

When autocomplete results are available use up and down arrows to review and enter to go to the desired page. Touch device users, explore by touch or with swipe gestures.

Recent Posts

  • vROps: Management Pack Troubleshooting
  • Windows AD {Active Directory} (PowerShell) samples
  • Migrate VMs Between vCentres Using Powershell or PowerCLI
  • Set VM Tools to Update Automatically on VM Reboot using powershell
  • Windows Administrator Must Have Powershell Commands

Recent Comments

  • JB on Script: How to get VM with Tag Assignment and export results to csv using PowerCLI or Powershell
  • DL on How to change VCSA root password and bypass BAD PASSWORD: it is based on a dictionary word for vCenter VCSA root account warning
  • 360coolp on How to change VCSA root password and bypass BAD PASSWORD: it is based on a dictionary word for vCenter VCSA root account warning
  • Yogesh on ESXi 8.x, 7.x, 6.x Service sfcbd-watchdog Not Running / Fails to Start – VirtuallyThatGuy
  • VirtuallyThatGuy on ESXi 8.x, 7.x, 6.x Service sfcbd-watchdog Not Running / Fails to Start – VirtuallyThatGuy

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019
  • May 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • December 2018
  • November 2018
  • October 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • January 2018
  • December 2017

Categories

  • Automation
  • PowerCLI
  • VMware
  • Windows
© 2025 VirtuallyThatGuy | Powered by Superbs Personal Blog theme