ForEach loop gather CPU core count

Hi Have the below code where I am trying to get a total count of CPU cores on numerous servers.
What it returns for $result is 2 columns one for Cores which is blank/empty & one with computernames.

Trying to figure out what I did wrong & is causing the core count to be missing.

My ultimate goal is to get a total count of cores returned.

I will probably also do a export-csv of $result so I can capture it in a file.

$2008List = Get-Content -Path C:\temp\2008-servers.txt 

if ( $null -eq $acred)
{$acred = get-Credential <myusername>}

$result = @()

foreach ($2008 in $2008List){
    $Session = New-PSSession -ComputerName $2008 -Credential $acred -ErrorAction SilentlyContinue 

    Invoke-Command -Session $Session -ScriptBlock { $cores = (Get-WmiObject -Class win32_processor ).NumberOfCores  } -ErrorAction Stop 
    
    $properties = @{Computer = $Session.ComputerName
                    Cores = $cores
                }
    $output = New-Object psobject -Property $properties

    $result += $output
    
    Remove-PSSession  $Session    
    
}

Write-Output $result

Hi, welcome back :wave:

Your variable $cores exits only within the scope of the script block, so your variable $cores in the hashtable doesn’t reference anything (or at least it doesn’t reference the result of your Get-WMIObject query).

Get-WMIObject is superseded by Get-CIMInstance. Consider something like this:

if ( $null -eq $acred ) {
    $acred = Get-Credential <myusername>
}

$results = foreach ($2008 in $2008List) {
    
    $CIMSession = New-CimSession -ComputerName $2008 -Credential $acred

    $cores = (Get-CimInstance -ClassName Win32_Processor -CimSession $CIMSession).NumberofCores
    
    [PSCustomObject] @{
        Computer = $2008
        Cores    = $cores
    }
   
    Remove-CimSession $CIMSession
    
}

Write-Output $results

There’s actually a much easier way for achieving what you’re after …

Since the WMI / CIM cmdlets are able to query remote computers by themselfs you don’t need Invoke-Command and you don’t need a loop.

$Cred = Get-Credential -UserName 'UserName'
$ComputerList = Get-Content -Path C:\temp\2008-servers.txt
$CimSessionList = New-CimSession -ComputerName $ComputerList -Credential $Cred
$result = Get-CimInstance -ClassName Win32_Processor -CimSession $CimSessionList

$result |
   Select-Object -Property PsComputerName,NumberOfCores,NumberOfLogicalProcessors
2 Likes

Hi,

The reason I used Get-WMIObject is because some of these old 2008 servers run PowerShell 2.0 & I believe the CIM commands came out in PowerShell 3.0

So should be defining $core prior to the foreach loop?

My main goal is to get a total core count of all the servers I loop through.

And I am sure you know … Server 2008 is way past end of life :slight_smile:

January 14, 2020

With my code suggestion you’re not running the commands on the remote computers!! :point_up_2:t4: If I’m not wrong it does not matter if they have PwSh at all. But it would take you only seconds to figure that out! :man_shrugging:t4: :wink:

You don’t need a loop. Neither with Get-WmiObject nor with Get-CimInstance!! Both cmdlets can handle multiple remote computers at once.

If you’ve collected the core count of all servers you can use

to get the sum of them. :wink: