Report on VMs vCPUs, Cores and Sockets

      No Comments on Report on VMs vCPUs, Cores and Sockets

From time to time we want to make sure our VMs are up to spec. So this report might help you.
Basically it will output a report CSV with name report_CPU_Socket.csv.
Obviously this can be changed by updating the last line in the script which is the responsible for redirecting the output to a file.

$array = @()
$vms = Get-view  -ViewType VirtualMachine
foreach ($vm in $vms) {
    $obj = new-object psobject
    $obj | Add-Member -MemberType NoteProperty -Name name -Value $vm.Name
    $obj | Add-Member -MemberType NoteProperty -Name TotalCores -Value $vm.config.hardware.NumCPU
    $obj | Add-Member -MemberType NoteProperty -Name TotalSockets -Value ($vm.config.hardware.NumCPU/$vm.config.hardware.NumCoresPerSocket)
    $obj | Add-Member -MemberType NoteProperty -Name CoresPersocket -Value $vm.config.hardware.NumCoresPerSocket
    $array += $obj
 
}
$array |Export-Csv .\report_CPU_Socket.csv -NoTypeInformation

This is great but how about if you have thousands of VMs and are only interested in a few set?
Well then you can filter them. By adjusting line 2 on the script;
$vms = Get-view -ViewType VirtualMachine |where {$_.name -like 'MS*'}

The above code will get all VMs that start with MS. So full code would look like this:

$array = @()
$vms = Get-view  -ViewType VirtualMachine |where {$_.name -like 'MS*'}
foreach ($vm in $vms) {
    $obj = new-object psobject
    $obj | Add-Member -MemberType NoteProperty -Name name -Value $vm.Name
    $obj | Add-Member -MemberType NoteProperty -Name TotalCores -Value $vm.config.hardware.NumCPU
    $obj | Add-Member -MemberType NoteProperty -Name TotalSockets -Value ($vm.config.hardware.NumCPU/$vm.config.hardware.NumCoresPerSocket)
    $obj | Add-Member -MemberType NoteProperty -Name CoresPersocket -Value $vm.config.hardware.NumCoresPerSocket
    $array += $obj
 
}
$array |Export-Csv .\report_CPU_Socket.csv -NoTypeInformation

You can use several filters within a single query just use -AND and -OR to customize the query to your needs
More on Powershell operators here

Leave a Reply

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