Skip to content

Instantly share code, notes, and snippets.

@gravejester
Last active Aug 29, 2015
Embed
What would you like to do?
function Get-Cpu {
<#
.SYNOPSIS
Get processor information from one or more computers.
.DESCRIPTION
This function uses CIM to query one or more computer for processor information.
It will first try to connect using 'WSMAN' protocol, but will fall back to using 'DCOM' if
this fails.
.EXAMPLE
Get-Cpu
Will query the local computer for processor information.
.EXAMPLE
Get-Cpu Server01 -Credential domain/user01
Will try to query 'Server01' with the credentials of 'domain/user01'.
.OUTPUTS
PSObject
.LINK
http://msdn.microsoft.com/en-us/library/aa394373%28v=vs.85%29.aspx
.NOTES
To determine if hyperthreading is enabled for the processor, compare LogicalProcessors and Cores.
If hyperthreading is enabled in the BIOS for the processor, then Cores is less than LogicalProcessors.
For example, a dual-processor system that contains two processors enabled for hyperthreading can run four threads or programs or simultaneously.
In this case, Cores is 2 and LogicalProcessors is 4.
Author: Øyvind Kallstad
Date: 01.12.2014
Version: 1.0
#>
[CmdletBinding()]
param (
# Specifies the target computer. Enter a fully qualified domain name, NetBIOS name, or an IP address. When the remote computer is in a different domain than the local computer,
# the fully qualified domain name is required.
[Parameter(Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName)]
[ValidateNotNullorEmpty()]
[string[]] $ComputerName = $env:COMPUTERNAME,
# Specifies a user account that has permission to perform this action.
[Parameter()]
[System.Management.Automation.Credential()] $Credential = [System.Management.Automation.PSCredential]::Empty,
# Use this parameter to force the creation of a new CIM session. The default behaviour is to save the session and reuse it if possible.
[Parameter()]
[switch] $ForceNewCimSession
)
process {
foreach ($computer in $ComputerName) {
try {
# define hashtable for Test-WSMan properties
$wsmanProperties = @{
ComputerName = $computer
ErrorAction = 'SilentlyContinue'
}
if ($PSBoundParameters['Credential']){
$wsmanProperties.Authentication = 'Basic'
$wsmanProperties.Credential = $Credential
}
# check if wsman is responding
$wsmanTest = Test-WSMan @wsmanProperties
# based on whether the result of the wsman test, we decide on the protocol to use for the cim session
if (-not([string]::IsNullOrEmpty($wsmanTest))) {
$cimSessionOption = New-CimSessionOption -Protocol Wsman
Write-Verbose 'Using protocol "Wsman"'
}
else {
$cimSessionOption = New-CimSessionOption -Protocol Dcom
Write-Verbose 'Using protocol "Dcom"'
}
# define hashtable for cim session properties
$cimSessionProperties = @{
Name = $computer
ComputerName = $computer
SessionOption = $cimSessionOption
Verbose = $false
ErrorAction = 'Stop'
}
if ($PSBoundParameters['Credential']){ $cimSessionProperties.Credential = $Credential}
# first check to see if a cim session already exist, if so use this - if not, we create a new one
# if -ForceNewCimSession is used, always create a new cim session
if ((-not($cimSession = Get-CimSession -Name $computer -ErrorAction SilentlyContinue)) -or ($ForceNewCimSession)) {
Write-Verbose 'Creating new CIM session'
$cimSession = New-CimSession @cimSessionProperties -SkipTestConnection
} else { Write-Verbose 'Using existing CIM session' }
# run query to get data
$processors = Get-CimInstance -CimSession $cimSession -Query 'SELECT * FROM Win32_Processor' -Verbose:$false -ErrorAction Stop
# iterate through the returned physical processors
foreach ($processor in $processors) {
# get processor architecture
switch ($processor.Architecture) {
0 {$processorArchitecture = 'x86'}
1 {$processorArchitecture = 'MIPS'}
2 {$processorArchitecture = 'Alpha'}
3 {$processorArchitecture = 'PowerPC'}
6 {$processorArchitecture = 'Itanium'}
9 {$processorArchitecture = 'x64'}
DEFAULT {$processorArchitecture = 'Unknown'}
}
# get processor status
switch ($processor.CpuStatus) {
0 {$cpuStatus = 'Unknown'}
1 {$cpuStatus = 'CPU Enabled'}
2 {$cpuStatus = 'Cpu Disabled by User via BIOS Setup'}
3 {$cpuStatus = 'CPU Disabled by BIOS (POST Error)'}
4 {$cpuStatus = 'CPU Is Idle'}
DEFAULT {$cpuStatus = 'Unknown'}
}
# get processor type
switch ($processor.ProcessorType) {
1 {$processorType = 'Other'}
2 {$processorType = 'Unknown'}
3 {$processorType = 'Central Processor'}
4 {$processorType = 'Math Processor'}
5 {$processorType = 'DSP Processor'}
6 {$processorType = 'Video Processor'}
DEFAULT {$processorType = 'Unknown'}
}
# CPU socket information, including the method by which this processor can be upgraded, if upgrades are supported
switch ($processor.UpgradeMethod) {
1 {$upgradeMethod = 'Other'}
2 {$upgradeMethod = 'Unknown'}
3 {$upgradeMethod = 'Daughter Board'}
4 {$upgradeMethod = 'ZIF Socket'}
5 {$upgradeMethod = 'Replacement or Piggy Back'}
6 {$upgradeMethod = 'None'}
7 {$upgradeMethod = 'LIF Socket'}
8 {$upgradeMethod = 'Slot 1'}
9 {$upgradeMethod = 'Slot 2'}
10 {$upgradeMethod = '370 Pin Socket'}
11 {$upgradeMethod = 'Slot A'}
12 {$upgradeMethod = 'Slot M'}
13 {$upgradeMethod = 'Socket 423'}
14 {$upgradeMethod = 'Socket A (Socket 462)'}
15 {$upgradeMethod = 'Socket 478'}
16 {$upgradeMethod = 'Socket 754'}
17 {$upgradeMethod = 'Socket 950'}
18 {$upgradeMethod = 'Socket 939'}
DEFAULT {$upgradeMethod = 'Unknown'}
}
# get processor availability
switch ($processor.Availability) {
1 {$processorAvailability = 'Other'}
2 {$processorAvailability = 'Unknown'}
3 {$processorAvailability = 'Running or Full Power'}
4 {$processorAvailability = 'Warning'}
5 {$processorAvailability = 'InTest'}
6 {$processorAvailability = 'Not Applicable'}
7 {$processorAvailability = 'Power Off'}
8 {$processorAvailability = 'Off Line'}
9 {$processorAvailability = 'Off Duty'}
10 {$processorAvailability = 'Degraded'}
11 {$processorAvailability = 'Not Installed'}
12 {$processorAvailability = 'Install Error'}
13 {$processorAvailability = 'Power Save - Unknown'}
14 {$processorAvailability = 'Power Save - Low Power Mode'}
15 {$processorAvailability = 'Power Save - Standby'}
16 {$processorAvailability = 'Power Cycle'}
17 {$processorAvailability = 'Power Save - Warning'}
DEFAULT {$processorAvailability = 'Unknown'}
}
$output = [PSCustomObject] [Ordered] @{
ComputerName = $computer
AddressWidth = $processor.AddressWidth
Architecture = $processorArchitecture
Availability = $processorAvailability
CpuStatus = $cpuStatus
CurrentClockSpeed = $processor.CurrentClockSpeed
CurrentVoltage = $processor.CurrentVoltage
DataWidth = $processor.DataWidth
Description = $processor.Description
DeviceID = $processor.DeviceID
Family = (Get-CpuFamily -Key $processor.Family)
L2CacheSize = $processor.L2CacheSize
L3CacheSize = $processor.L3CacheSize
Manufacturer = $processor.Manufacturer
MaxClockSpeed = $processor.MaxClockSpeed
Name = $processor.Name
Cores = $processor.NumberOfCores
LogicalProcessors = $processor.NumberOfLogicalProcessors
ProcessorType = $processorType
Role = $processor.Role
SocketDesignation = $processor.socketDesignation
Status = $processor.Status
UpgradeMethod = $upgradeMethod
}
$defaultProperties = @('ComputerName','Name','Cores','LogicalProcessors','Status')
$defaultDisplayPropertySet = New-Object System.Management.Automation.PSPropertySet('DefaultDisplayPropertySet',[string[]]$defaultProperties)
$PSStandardMembers = [System.Management.Automation.PSMemberInfo[]]@($defaultDisplayPropertySet)
$output | Add-Member -MemberType MemberSet -Name PSStandardMembers -Value $PSStandardMembers -Force
Write-Output $output
Remove-Variable -Name processorArchitecture,processorAvailability,cpuStatus,processorType,upgradeMethod -ErrorAction SilentlyContinue
}
}
catch {
# if the connection fail we don't want to keep the session
Remove-CimSession -Name $computer -ErrorAction SilentlyContinue
Write-Warning "At line:$($_.InvocationInfo.ScriptLineNumber) char:$($_.InvocationInfo.OffsetInLine) Command:$($_.InvocationInfo.InvocationName), Exception: '$($_.Exception.Message.Trim())'"
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment