PowerShell Script: Hyper Threading Status of Window Server

Got a question on Stackoverflow.com about showing the Hyper Threading inside a HTML System Report. The code starts off by using the Get-WmiObject with the switch Win32_Processor. This allows us to get the following output:

Caption : Intel64 Family 6 Model 60 Stepping 3
DeviceID : CPU0
Manufacturer : GenuineIntel
MaxClockSpeed : 3501
Name : Intel(R) Core(TM) i7-4770K CPU @ 3.50GHz
SocketDesignation : SOCKET 1150

Then we use Get-WmiObject Win32_Processor | Get-Member to grab a list of properties that we can use. To get the Hyper Threading status we only care about NumberOfCores and NumberOfLogicalProcessors. We go on to use the Measure-Object to gather the sum of the NumberOfCores and NumberOfLogicalProcessors.

$vCores = Get-WmiObject Win32_Processor | Measure -Property  NumberOfCores -Sum
$vCores = $vCores.Sum
$vLogicalCPUs = Get-WmiObject Win32_Processor | Measure -Property  NumberOfLogicalProcessors -Sum
$vLogicalCPUs = $vLogicalCPUs.sum

Now we create the IF statement that tells us if the Hyper Threading is Enabled or Disabled.

if ($vLogicalCPUs -gt $vCores) { 
       $HT=“Hyper Threading: Enabled”
    } 
   else {  $HT=“Hyper Threading: Disabled”
    }

The challenge was getting the above IF statement into a HTML header with output. To do this we created a standard empty array $HyperThreading = @(). This array will allow us to store the information into a readable array. After the array is created we need to create a variable that will add a header and the result, and to do this we use: Add-Member -membertype noteproperty -Name "Hyper_Threading" -Value "$HT" -Force giving us the output:

Hyper_Threading
—————
Hyper Threading: Enable

We simply add the empty array and combing the variable. $HyperThreading += $HThreading

Now we have a working script that will give us the following output:

Hyper Threading Information

Hyper Threading: Enabled

You can get the full code here:

$vCores = Get-WmiObject Win32_Processor | Measure -Property  NumberOfCores -Sum
$vCores = $vCores.Sum
$vLogicalCPUs = Get-WmiObject Win32_Processor | Measure -Property  NumberOfLogicalProcessors -Sum
$vLogicalCPUs = $vLogicalCPUs.sum
$HyperThreading = @()
    if ($vLogicalCPUs -gt $vCores) { 
       $HT=“Hyper Threading: Enabled”
    } 
   else {  $HT=“Hyper Threading: Disabled”
    }
	$HThreading = New-Object psobject
    $HThreading | Add-Member -membertype noteproperty -Name "Hyper_Threading" -Value "$HT" -Force
    $HyperThreading += $HThreading
    $HyperThreading | ConvertTo-Html -title "Hyper Threading" -body "<H2>Hyper Threading Information</H2>" | 
    Set-Content F:\Test\Hyper_Threading.html
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s