...hopefully some useful VMware related stuff

Display number of vCPUs assigned and vCPU to Core ratio

17/05/2010 12:00

Script displays Overall number of Cores available over all hosts, then the number of Virtual CPUs assigned and then the ratio.

 

The script can be run from a standard PowerShell window, as it checks for the VMware snapin and loads if necessary. If no connection exists to vCenter server it will prompt for connection details.

 

Following that a table showing the individual assignments for individual hosts.

 

 

Script can be downloaded HERE and is shown below:

 

# List CPU assignment and vCPUs to Cores Ratio
# https://www.a2-alpha.co.uk
# 20100517

#Check for VMware PS Snapin - load and initialise if not

$erroractionpreference = "silentlycontinue"

$snapins = get-pssnapin | select Name
foreach($snapin in $snapins)
{
        if($snapin.Name -eq "VMware.VimAutomation.Core"){
        $result = "Yes"
    }
    else
    {
        $result = "No"
    }
}
if($result -eq "No")
{
    Add-PSsnapin VMware.VimAutomation.Core
    Initialize-VIToolkitEnvironment.ps1
}

#Test if connected to viserver or not

$Error.clear()

get-vm | out-null

$E = $Error
$E = $E | out-string
if($E.Contains("Get-VM"))
{
    $vcserver = read-host "Enter vCenter name or ESX host name"
    $us = read-host "Enter Username"
    $pw = read-host "Enter Password" -assecurestring:$true
    $cred = New-Object System.Management.Automation.PSCredential -ArgumentList $us,$pw
    Connect-VIServer -Server $vcserver -Credential $cred | out-null
    [int]$connectionstate = 0
}

#Clear Screen ready for results

clear
" "
"** vCPU Assignment and vCPU to physical Cores Ratio **"
" "

#Get all powered on VMs and get CPUs

$clusters = get-cluster | sort Name
foreach($cluster in $clusters)
{
"Cluster: $cluster"
" "
$vmon = get-vm -location $cluster | where {$_.PowerState -eq "PoweredOn"}
$vmoncount = $vmon.Count
$vmoncpus = get-vm -location $cluster | where {$_.PowerState -eq "PoweredOn"} | select Name, NumCPU
$cputotal = 0

#Run through each VM and count CPUs

foreach($vmoncpu in $vmoncpus)
{
      [int]$number = $vmoncpu.NumCPU
      $cputotal += $number
}
"$cputotal `t Total CPUs assigned across all hosts"


#Get hosts then run through each to count number of cores

$vmhosts = get-vmhost -location $cluster | get-view | sort Name
$cpucoretotal = 0
foreach($vmhost in $vmhosts)
{
      [int]$cores = $vmhost.Hardware.CpuInfo.NumCpuCores
      $cpucoretotal += $cores
}
"$cpucoretotal `t Total CPU cores available across all hosts"
$virt2logratio = [math]::round(($cputotal)/($cpucoretotal),2)
"$virt2logratio `t Ratio of vCPUs assigned to Physical Cores Available"
" "

#Create table for cpu information within each host

$Report = @()
ForEach($vmhost in $vmhosts)
{
      $vmsrunningperhost = (Get-vmhost $VMHost.name | get-vm | where {$_.Powerstate -eq "PoweredON"})
      $runningperhostcount = (Get-vmhost $vmhost.name | get-vm | where {$_.Powerstate -eq "PoweredON"} | Measure-Object).Count
      $number = 0
      $cputotal = 0
      foreach($vm in $vmsrunningperhost)
      {
              [int]$number = $vm.NumCPU
              $cputotal += $number
      }
      $corestotal = $vmhost.Hardware.CpuInfo.NumCpuCores
      $hostvirt2logratio = [math]::round(($cputotal)/($corestotal),2)

      $ReportObj = "" | Select "ESX Host", "Physical Cores Total", "vCPUs Assigned", "Ratio vCPU per Core", "Number of Running VMs"
      $ReportObj."ESX Host" = $vmhost.Name
      $ReportObj."Physical Cores Total" = $corestotal
      $ReportObj."vCPUs Assigned" = $cputotal
      $ReportObj."Ratio vCPU per Core" = $hostvirt2logratio
      $ReportObj."Number of Running VMs" = $runningperhostcount
      $Report += $ReportObj
}
$Report | format-table
}
#Disconnect after results displayed if it was not connected when script was run

if($connectionstate -eq 0)
{
     disconnect-viserver -confirm:$false
}
 

 

Topic: Display number of vCPUs assigned and vCPU to Core ratio

No comments found.

Search site