This function will test the hardware form factor to see if it matches the requested type. If the type matches the function will return $true
otherwise $false
if it does not match.
<#
.SYNOPSIS
Test if the hardware is of a certain type.
.DESCRIPTION
Test if the hardware is in the desktop, laptop or server form factor. Optionally check if the OS running is a client OS.
.PARAMETER Type
The hardware type to check for.
#>
function Test-HardwareFormFactor {
[cmdletbinding()]
param (
# The prepend to add to the PC name
[ValidateNotNullOrEmpty()]
[Parameter(Mandatory=$true,HelpMessage="The form factor to test for")]
[ValidateSet('Server','Rackmount','Blade','Tower','Desktop','Laptop','Tablet','Virtual')]
[String]
$Type
)
# Get the chassis types with WMI
Write-Verbose -Message 'Getting ChassisTypes with WMI...'
try {
$ChassisTypes = (Get-CimInstance -ClassName Win32_SystemEnclosure -ErrorAction Stop | Select-Object -ExpandProperty ChassisTypes -ErrorAction Stop)
}
catch {
$Message = $_.Exception.message
Write-Error -Message 'Exception running WMI query:'
Write-Error -Message $Message
return $False
}
# Make sure there was an object returned
if ( $ChassisTypes ) {
Write-Verbose -Message "Got ChassisTypes value: $ChassisTypes"
} else {
Write-Verbose -Message 'WMI query returned no object'
return $False
}
# Get the computer system model to check if virtual
Write-Verbose -Message 'Checking if system is virtual...'
try {
$System = (Get-CimInstance -ClassName Win32_ComputerSystem -ErrorAction Stop)
}
catch {
$Message = $_.Exception.message
Write-Error -Message 'Exception running WMI query:'
Write-Error -Message $Message
return $False
}
# Test the system model
if ( $System.Model -match 'Virtual' ) {
Write-Verbose -Message "System model '$($System.Model) indicates the system is virtual"
if ( $Type -eq 'Virtual' ) {
return $True
}
}
# Skip other checks if the type is virtual and the above tests did not pass
if ( $Type -eq 'Virtual' ) {
Write-Verbose -Message 'System type does not appear to be virtual'
return $False
}
# Attempt to get PCSystemType first
Write-Verbose -Message 'Getting the PCSystemType...'
switch ( $System.PCSystemType ) {
'1' {
Write-Verbose -Message 'PCSystemType indicates system is a desktop'
$PCSystemType = 'Desktop'
}
'2' {
Write-Verbose -Message 'PCSystemType indicates system is a mobile device (tablet/laptop/notebook)'
$PCSystemType = 'Mobile'
}
'3' {
Write-Verbose -Message 'PCSystemType indicates system is a workstation'
$PCSystemType = 'Desktop'
}
'4' {
Write-Verbose -Message 'PCSystemType indicates system is a server'
$PCSystemType = 'Server'
}
'5' {
Write-Verbose -Message 'PCSystemType indicates system is a SOHO server'
$PCSystemType = 'Server'
}
'6' {
Write-Verbose -Message 'PCSystemType indicates system is an appliance'
$PCSystemType = 'Desktop'
}
'7' {
Write-Verbose -Message 'PCSystemType indicates system is a performance server'
$PCSystemType = 'Server'
}
default {
Write-Verbose -Message 'PCSystemType is unknown'
$PCSystemType = 'Unknown'
}
}
# Skip further testing for desktop and server if the PCSystemType matches
if ( $Type -eq 'Desktop' -and $PCSystemType -eq 'Desktop' ) {
Write-Verbose -Message 'Returning true due to PCSystemType being set to desktop'
return $True
}
if ( $Type -eq 'Server' -and $PCSystemType -eq 'Server' ) {
Write-Verbose -Message 'Returning true due to PCSystemType being set to server'
return $True
}
# Determine system type from the ChassisTypes value
Write-Verbose -Message 'Testing chassis type with ChassisTypes value...'
if ( $ChassisTypes | Where-Object { $_ -match '^(8|9|10|14)$' } ) { $ChassisType = 'Laptop' }
elseif ( $ChassisTypes | Where-Object { $_ -match '^(3|4|13|16|24|34|35|36)$' } ) { $ChassisType = 'Desktop' }
elseif ( $ChassisTypes | Where-Object { $_ -match '^(11|30)$' } ) { $ChassisType = 'Tablet' }
elseif ( $ChassisTypes | Where-Object { $_ -match '^(7|8)$' } ) { $ChassisType = 'Tower' }
elseif ( $ChassisTypes | Where-Object { $_ -match '^(28|29)$' } ) { $ChassisType = 'Blade' }
elseif ( $ChassisTypes | Where-Object { $_ -match '^(5|23)$' } ) { $ChassisType = 'Rackmount' }
else {
Write-Verbose -Message 'Could not determine chassis type, returning false'
return $False
}
Write-Verbose -Message "Determined chassis type is $ChassisType"
# Compare the type and return value
if ( $Type -eq $ChassisType ) {
Write-Verbose -Message 'Chassis type matches, returning true'
return $True
} else {
Write-Verbose -Message "Chassis type $Type does not match actual chassis type $ChassisType"
return $False
}
}
To test if the system is a laptop:
Test-HardwareFormFactor -Type Laptop
To test if the system is a desktop:
Test-HardwareFormFactor -Type Desktop