Reputation: 53
I'm currently working on a powershell script that should retrieve the type of machine i'm working on and i've looked at several examples already but i can't get it to work properly. I want to retrieve my chassis type so i can determine if i'm working on a laptop or desktop.
This is my code:
$isLaptop = [bool](Get-WmiObject -Class Win32_SystemEnclosure -Property
ChassisTypes | Where-Object ChassisTypes -in '{9}', '{10}', '{14}')
The problem is that even when i'm running the script on a desktop i'm getting a response which is true although the chassis type of my desktop is {3}.
I'm probably overlooking something stupid but would be nice if someone could give me some assistance here i'm kinda new to powershell aswell.
Upvotes: 1
Views: 12660
Reputation: 2293
This code is taken from here and made into a function. It creates a hash table and translates the values to a human-readable form.
I've tested this in 5.1 and 7.1 and works for both of those. So it's future proof for a while yet.
Function Get-ChassisType() {
$ChassisTypes = @{
Name = 'ChassisType'
Expression = {
# property is an array, so process all values
$result = foreach($value in $_.ChassisTypes)
{
switch([int]$value)
{
1 {'Other'}
2 {'Unknown'}
3 {'Desktop'}
4 {'Low Profile Desktop'}
5 {'Pizza Box'}
6 {'Mini Tower'}
7 {'Tower'}
8 {'Portable'}
9 {'Laptop'}
10 {'Notebook'}
11 {'Hand Held'}
12 {'Docking Station'}
13 {'All in One'}
14 {'Sub Notebook'}
15 {'Space-Saving'}
16 {'Lunch Box'}
17 {'Main System Chassis'}
18 {'Expansion Chassis'}
19 {'SubChassis'}
20 {'Bus Expansion Chassis'}
21 {'Peripheral Chassis'}
22 {'Storage Chassis'}
23 {'Rack Mount Chassis'}
24 {'Sealed-Case PC'}
default {"$value"}
}
}
$result
}
}
return (Get-CimInstance -ClassName Win32_SystemEnclosure | Select-Object -Property $ChassisTypes).ChassisType
}
Get-ChassisType
# When using a desktop.
# >PS ((Get-ChassisType) -eq "Desktop")
# >PS True
# When using a desktop.
# >PS Get-ChassisType
# >PS Desktop
# When using a laptop.
# >PS Get-ChassisType
# >PS Laptop
Upvotes: 0
Reputation: 19684
This is meant to be an alternative to Mark's answer for readability:
#requires -Version 3
$isLaptop = [bool](Get-WmiObject -Class Win32_SystemEnclosure |
Where-Object ChassisTypes -in '{9}', '{10}', '{14}')
After trying to reproduce your problem, however, you're not working with the right types. While the output of the command looks like this:
ChassisTypes : {3}
what you actually have is an array of UInt16
there. So what you need to compare to is one collection to another:
[uint16[]]$laptop = 9, 10, 14
$isLaptop = foreach ($chassis in (Get-CimInstance -ClassName Win32_SystemEnclosure).ChassisTypes) {
if ($chassis -in $laptop) {
$true
break
}
}
And broken down a little further:
#requires -Version 4
$isLaptop = [bool](Get-CimInstance -ClassName Win32_SystemEnclosure).
ChassisTypes.
Where({ $PSItem -in 9, 10, 14 })
Upvotes: 3
Reputation: 23385
Try this:
$ChassisType = (Get-CimInstance -ClassName Win32_SystemEnclosure).ChassisTypes
if ($ChassisType -eq 9 -or $ChassisType -eq 10 -or $ChassisType -eq 14) {
$isLaptop = $true
}
else {
$isLaptop = $false
}
$isLaptop
Upvotes: 0