SQL Server Power Search - Get More Relevant Results

Physical CPU Count

How to get the physical CPU count for a server using PowerShell and WMI...

 

It is a very common requirement for licensing reasons that you know exactly how many physical CPUs there are in a server.

Checking Task Manager, looking at Device Manager, running sysinfo32, all report the number of cores and logical processors. There is no easy way to find out the number of sockets without installing specific software or having a look at the motherboard.

The problem is worse when you have several hundred servers to check, a situation I was placed in recently.

After a lot of research I discovered that there is a way using WMI. The Win32_Processor object has a property called SocketDesignation which is unique for the first core in each socket. The MSDN documentation describes it as "Type of chip socket used on the circuit.", but every one I've looked at seems to be more like a socket identifier. Assuming this is always the case, you can get the physical CPU count by counting the number of unique values.

I have written a PowerShell function that does this, note that it also takes the number of cores per processor into account for later versions of Windows:

function Get-CPUs {
    param ($server, $username)
    if ($username)
    {
        $processors = get-wmiobject -computername $server -credential $username win32_processor
    }
    else
    {
        $processors = get-wmiobject -computername $server win32_processor
    }
    if (@($processors)[0].NumberOfCores)
    {
        $cores = @($processors).count * @($processors)[0].NumberOfCores
    }
    else
    {
        $cores = @($processors).count
    }
    $sockets = @(@($processors) |
    % {$_.SocketDesignation} |
    select-object -unique).count;

    "Cores: $cores, Sockets: $sockets";
}

This function takes 2 parameters. The first is mandatory, and is the name or IP address of the server. The second is optional, and is the username to use when the account you are currently using does not have access to the server. This will display a dialog asking for the password.

Running this manually for multiple servers is going to take some time, and I use an xml file containing server and instance configuration. I will show how to read from this file, and automate the retrieval of the physical CPU counts across the entire estate in my next PowerShell article.

I have had feedback that this works fine on Windows 2003, but returns incorrect results on Windows 2008. I do not currently have the chance to investigate, but I will do so as soon as possible.