We recently needed to calculate the number of physical CPUs in use by hosts running Windows VMs in order to work out how many Windows Server 2008 Data Center Edition licenses we require.

Data Center (DC) edition permits you to run as many Windows Server VMs as you like on a host which has the DC license. You don’t actually need to install the Datacenter software, so you can use this license on a host running vSphere or Xen (as well as Hyper-V).

Not all of our hosts need a DC license as some don’t run any Windows VMs, and other run a smaller number where it’s cheaper either to use an Enterprise license (up to 4 VMs) or license VMs individually. To mark which hosts need a DC license, I create a Custom Attribute called “DC SPLA” for all hosts, with values of “Yes”,”No”, or “?” (where it wasn’t clear what license was appropriate).

I then created a small PowerCLI script which dumps out a CSV file containing all hosts which were possible DC license uses (those which were marked “Yes” or “?”), along with the number of physical CPUs in each host.

It’s a short script and isn’t particularly special but I’m posting it anyway.

  1. $vcentername = "myvcentername.net"
  2. $outputfile = "c:\users\myname\Desktop\cpucount.csv"
  3.  
  4. Connect-VIServer $vcentername
  5.  
  6. get-vmhost | ForEach-Object { 
  7.  if ((Get-annotation -Entity $_ -CustomAttribute "DC SPLA" | select -ExpandProperty value ) -ne "No")
  8.  {
  9.  New-Object -TypeName PSobject -Property @{
  10.   HostName = $_.name 
  11.   CpuCount = (Get-View $_.ID).Hardware.CpuInfo.NumCpuPackages
  12.   	}| Select-Object Hostname,CpuCount
  13.  }
  14. } | export-csv -Path $outputfile 
  15.  
  16. Disconnect-VIServer $vcentername -Confirm:$false