Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, December 15, 2011 9:21 AM
hi,
I was surprised at how many discussions there are of correct way of doing this, some quite complex.
I came up with the following very simple code that seems to work in MY ENVIRONMENT - can u pls tell if can be made tighter or if you think it will fail for n>4 cpu boxes?
param($target)
$list=get-wmiobject -computername $target win32_processor -Property "deviceid"
$array=foreach ($result in $list) {$cntCPUs++}
$cntCPUs
*VMs also return correct number - in my case host is HV and there are 1 or 2 CPUs.
roys99
All replies (26)
Thursday, December 15, 2011 10:07 AM ✅Answered
Try this one:
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";
}
http://www.sql-server-pro.com/physical-cpu-count.html
Thursday, December 15, 2011 1:28 PM ✅Answered
try this, it's not clean but its another approach... I've never seen the performance counters be incorrect.
((get-counter "\Processor(*)\ idle time").countersamples | select instancename).length -1
Thursday, December 15, 2011 4:02 PM ✅Answered | 1 vote
Hi,
Another way may be:
gwmi Win32_ComputerSystem | select NumberOfProcessors, NumberOfLogicalProcessors
Sunday, December 18, 2011 8:52 AM ✅Answered
(Get-WmiObject Win32_ComputerSystem -Computername serverName).numberofprocessors
is consistent with my other results - in my environment i do not care for w2k3 any longer.
roys99
Thursday, December 15, 2011 9:52 AM
The reason for numerous discussions is that win32_processor doesn't always report correctly. It does most of the time, but still gets it wrong sometimes.
For example, I have 2 servers, each with 2 processors. Server1 has 4 cores per processor and Server2 has 2 cores per processor. Yet win32_processor returns '2' for Server1, and '8' for Server2. Go figure!
([string](0..9|%{[char][int](32+("39826578846355658268").substring(($_*2),2))})).replace(' ','')
Thursday, December 15, 2011 10:37 AM
I got excited there for a moment, because your code returned the correct numbers for Server2, but got it wrong for Server1. Sorry, no cigar.([string](0..9|%{[char][int](32+("39826578846355658268").substring(($_*2),2))})).replace(' ','')
Thursday, December 15, 2011 11:39 AM
Grrr. Can you post the raw data for your Server1 processor object. I want to poke at this one now. Obviously omit any sensitive info.
Thursday, December 15, 2011 12:17 PM
PS C:\Users\xxxx> gwmi -cl win32_processor
__GENUS : 2
__CLASS : Win32_Processor
__SUPERCLASS : CIM_Processor
__DYNASTY : CIM_ManagedSystemElement
__RELPATH : Win32_Processor.DeviceID="CPU0"
__PROPERTY_COUNT : 48
__DERIVATION : {CIM_Processor, CIM_LogicalDevice, CIM_LogicalElement, CIM_ManagedSystemElement}
__SERVER : SERVER1
__NAMESPACE : root\cimv2
__PATH : \SERVER1\root\cimv2:Win32_Processor.DeviceID="CPU0"
AddressWidth : 64
Architecture : 9
Availability : 3
Caption : Intel64 Family 6 Model 26 Stepping 5
ConfigManagerErrorCode :
ConfigManagerUserConfig :
CpuStatus : 1
CreationClassName : Win32_Processor
CurrentClockSpeed : 2261
CurrentVoltage : 12
DataWidth : 64
Description : Intel64 Family 6 Model 26 Stepping 5
DeviceID : CPU0
ErrorCleared :
ErrorDescription :
ExtClock : 5860
Family : 179
InstallDate :
L2CacheSize : 1024
L2CacheSpeed : 2261
L3CacheSize : 8192
L3CacheSpeed : 0
LastErrorCode :
Level : 6
LoadPercentage : 4
Manufacturer : GenuineIntel
MaxClockSpeed : 2261
Name : Intel(R) Xeon(R) CPU E5520 @ 2.27GHz
NumberOfCores : 4
NumberOfLogicalProcessors : 8
OtherFamilyDescription :
PNPDeviceID :
PowerManagementCapabilities :
PowerManagementSupported : False
ProcessorId : BFEBFBFF000106A5
ProcessorType : 3
Revision : 6661
Role : CPU
SocketDesignation : CPU1
Status : OK
StatusInfo : 3
Stepping : 5
SystemCreationClassName : Win32_ComputerSystem
SystemName : SERVER1
UniqueId :
UpgradeMethod : 9
Version : Model 10, Stepping 5
VoltageCaps :
__GENUS : 2
__CLASS : Win32_Processor
__SUPERCLASS : CIM_Processor
__DYNASTY : CIM_ManagedSystemElement
__RELPATH : Win32_Processor.DeviceID="CPU1"
__PROPERTY_COUNT : 48
__DERIVATION : {CIM_Processor, CIM_LogicalDevice, CIM_LogicalElement, CIM_ManagedSystemElement}
__SERVER : SERVER1
__NAMESPACE : root\cimv2
__PATH : \SERVER1\root\cimv2:Win32_Processor.DeviceID="CPU1"
AddressWidth : 64
Architecture : 9
Availability : 3
Caption : Intel64 Family 6 Model 26 Stepping 5
ConfigManagerErrorCode :
ConfigManagerUserConfig :
CpuStatus : 0
CreationClassName : Win32_Processor
CurrentClockSpeed : 2261
CurrentVoltage :
DataWidth : 64
Description : Intel64 Family 6 Model 26 Stepping 5
DeviceID : CPU1
ErrorCleared :
ErrorDescription :
ExtClock :
Family :
InstallDate :
L2CacheSize : 1024
L2CacheSpeed : 2261
L3CacheSize : 8192
L3CacheSpeed : 0
LastErrorCode :
Level : 6
LoadPercentage : 1
Manufacturer : GenuineIntel
MaxClockSpeed : 2261
Name : Intel(R) Xeon(R) CPU E5520 @ 2.27GHz
NumberOfCores : 4
NumberOfLogicalProcessors : 8
OtherFamilyDescription :
PNPDeviceID :
PowerManagementCapabilities :
PowerManagementSupported : False
ProcessorId :
ProcessorType :
Revision : 6661
Role : CPU
SocketDesignation :
Status : OK
StatusInfo : 3
Stepping : 5
SystemCreationClassName : Win32_ComputerSystem
SystemName : SERVER1
UniqueId :
UpgradeMethod : 9
Version : Model 10, Stepping 5
VoltageCaps :
________________________________________________________________________________________________________________________________________________________
([string](0..9|%{[char][int](32+("39826578846355658268").substring(($_*2),2))})).replace(' ','')
Thursday, December 15, 2011 12:26 PM
When I run your code on this server, this is the result:
PS C:\Users\xxxx> Get-CPUs server1
Cores: 8, Sockets: 1
([string](0..9|%{[char][int](32+("39826578846355658268").substring(($_*2),2))})).replace(' ','')
Thursday, December 15, 2011 12:29 PM
sorry if i am not up to speed but what is wrong with my "primitive" way of counting the # deviceID records?roys99
Thursday, December 15, 2011 1:02 PM
sorry if i am not up to speed but what is wrong with my "primitive" way of counting the # deviceID records? roys99
Nothing is wrong with it in principle. It is just that in practice, what the win32_processor class returns doesn't always correspond with reality.
As I said before, often it does, but often doesn't cut it. We need something we can rely on, and the win32_processor class is not that thing.
IMO, there is no scripting way to get this information reliably, because they all depend ultimately on that same WMI class. Using manufacturer's utilities is the surest route. (e.g. Intel's CPUID)
([string](0..9|%{[char][int](32+("39826578846355658268").substring(($_*2),2))})).replace(' ','')
Thursday, December 15, 2011 1:29 PM
Could your problem be hyperthreading related, I.E. you may see x, but realistically you have y.
Thursday, December 15, 2011 1:30 PM
As BigTeddy noted, the changes in CPU/Processor technology is so fast and diverse the WMI classes do not get redefined to account for all of these changes. Hence, his comment about using their custom utilities to get the only accurate count. When you absolutely have to know the correct value WMI falls short.
Thursday, December 15, 2011 2:33 PM
what is the OS? I know that some where along the lines the OS started to
handle this differently.
I know with a 2k3 sp2 system I have it shows all physical/logical CPUs but
with a 2k8 it breaks it down and the win32_processor has a numberofcores...
but, with the 2k3 system it has socketdesignation, which, im not sure helps
the case...
wmi win32_processor | ft deviceid,socketdesignation -auto
deviceid socketdesignation
CPU0 PROC_1
CPU1 PROC_2
CPU2 PROC_1
CPU3 PROC_1
so, this doesn’t help a ton, other than to say, its OS specific... also the
HT/Core matters too.. I have some CPU's that have cores and HT... sorta
weird... but whatever.... in 2k3 HT is considered the logical, and a core is
considered physical (don’t quote me) where as in 2k8 is knows about all
3....
Justin Rich
http://jrich523.wordpress.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Thursday, December 15, 2011 2:45 PM
I think that's a good point, Justin. My 'test' machines are Server1 (Win 2008) and Server2 (Win 2003).([string](0..9|%{[char][int](32+("39826578846355658268").substring(($_*2),2))})).replace(' ','')
Thursday, December 15, 2011 4:12 PM
I don’t think that works in 2k3... only win7/2k8
Justin Rich
http://jrich523.wordpress.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Thursday, December 15, 2011 4:17 PM
Another post to support the distinction between 2k3 and vista (and later):
A switch, based on the OS, would be the best way to approach this. In short, there is no one liner (unless it is very long) that will accurately get close.
Sunday, December 18, 2011 9:00 AM
Yes, in the one test I did do with this class (on 2008), it returned the correct results.([string](0..9|%{[char][int](32+("39826578846355658268").substring(($_*2),2))})).replace(' ','')
Wednesday, April 25, 2012 10:45 PM
I know I am a little late to the show, but here is some additional information for those still looking for a solution:
http://support.microsoft.com/kb/932370
Now to convince the owners of the 200+ Windows 2003 servers to let me install this patch just so I can run WMI queries on their servers. I'm thinking they are going to frown on it...
Nelson
Thursday, April 26, 2012 12:16 PM | 1 vote
couldn’t you query the processor model and then figure it out from there?
(gwmi win32_processor).name
Intel(R) Xeon(R) CPU E31225 @ 3.10GHz
http://ark.intel.com/products/52270/Intel-Xeon-Processor-E3-1225-(6M-Cache-3_10-GHz)
could easily scrape that page, and the divide the number you've got by the
number of cores...
I think if you dig around on the Intel site they've got a chart some place
you could use to compare rather than try to query that URL for each proc
type...
I believe AMD does that as well...
not easy, but should be more fun than trying to convince them to update 200
servers :)
the other option (depending on how you run this script), is to gather the
info and then figure out the core count (ie don’t automate the web search
cuz you'll probably only have a handful of proc types) or at least you can
figure out how to create the web search/scrape based on the proc set you've
got..
ps: I didn’t read the whole thread so this may already be discussed in there
:)
Justin Rich
http://jrich523.wordpress.com
PowerShell V3 Guide (Technet)
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Thursday, April 26, 2012 1:20 PM | 1 vote
Cool - thanks for the advice.
Nelson
Thursday, August 15, 2013 6:48 PM
I know it is a very old post, but here is my little contribution
1) Get-WmiObject -Class Win32_processor will return an array, if multiple physical CPU (socket) are found
$cpu = @(Get-WmiObject -Class Win32_processor -Computername $Computer -ea stop)
$c_socket = $cpu.count
$c_core = $cpu[0].NumberOfCores * $c_socket
$c_logical = $cpu[0].NumberOfLogicalProcessors * $c_socket
I force Get-Wmi to return an array even if only 1 CPU is return
$c_socket is the # of of physical CPU
$c_core and $c_logical are the total of core and logical cpu
Life is short, Enjoy it now. Cyreli
Thursday, February 1, 2018 2:27 PM
Please, don't suggest "english-only" solutions, because it's so annoying. For example, there is no "\Processor(*)\ idle time" counters on my machine, because I have non-US interface language. Please, examine your own docs: https://support.microsoft.com/en-us/help/287159/using-pdh-apis-correctly-in-a-localized-language
Thursday, February 1, 2018 2:32 PM
This is an English language forum. Please post in the forum for your language for assistance with other languages.
\(ツ)_/
Thursday, February 1, 2018 4:03 PM
Juse use $env:NUMBER_OF_PROCESSORS environment variable
Thursday, February 1, 2018 4:22 PM
Juse use $env:NUMBER_OF_PROCESSORS environment variable
Not the number of "physical" processors. It is the number of logical processors which, on a hyper-threaded processor, will be twice the number of physical processors and will also be dependent on the number of cores.
Get-WmiObject -Class Win32_processor |select NumberOfCores, NumberOfLogicalProcessors
$env:NUMBER_OF_PROCESSORS is the "NumberOfLogicalProcessors".
The following gives the number of physical processors (chips/modules).
@(Get-WmiObject -Class Win32_processor).Count
\(ツ)_/