Just ran across the need to determine what kind of machine I am running a script on. I needed to detect whether or not the machine was a laptop or desktop. Just so happens that WMI provides a nifty class for this.
As you check out the function below, notice some of the really odd chassis types available. How many of you have a "pizza box" or "lunchbox" chassis?
Function getChassisType(sstrComputer)
'Global: Dim intChassisType to reference by number
'Returns a string value of the type.
Set sobjWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & sstrComputer & "\root\cimv2")
Set colChassis = sobjWMIService.ExecQuery _
("Select ChassisTypes from Win32_SystemEnclosure")
For Each objChassis in colChassis
For i = Lbound(objChassis.ChassisTypes) to Ubound(objChassis.ChassisTypes)
intChassisType = objChassis.ChassisTypes(i)
Next
Next
Select Case intChassisType
Case 1
getChassisType = "Other"
Case 2
getChassisType = "Unknown"
Case 3
getChassisType = "Desktop"
Case 4
getChassisType = "Low Profile Desktop"
Case 5
getChassisType = "Pizza Box"
Case 6
getChassisType = "Mini Tower"
Case 7
getChassisType = "Tower"
Case 8
getChassisType = "Portable"
Case 9
getChassisType = "Laptop"
Case 10
getChassisType = "Notebook"
Case 11
getChassisType = "Hand Held"
Case 12
getChassisType = "Docking Station"
Case 13
getChassisType = "All in One"
Case 14
getChassisType = "Sub Notebook"
Case 15
getChassisType = "Space-Saving"
Case 16
getChassisType = "Lunch Box"
Case 17
getChassisType = "Main System Chassis"
Case 18
getChassisType = "Expansion Chassis"
Case 19
getChassisType = "SubChassis"
Case 20
getChassisType = "Bus Expansion Chassis"
Case 21
getChassisType = "Peripheral Chassis"
Case 22
getChassisType = "Storage Chassis"
Case 23
getChassisType = "Rack Mount Chassis"
Case 24
getChassisType = "Sealed-Case PC"
Case Else
getChassisType = "Unknown"
End Select
Set sobjWMIService = Nothing
End Function
Enjoy the code and as always, let me know if this script helps you out!
-Corey Thomas
MCSE/MCSA/MCDBA/Security+,CIW Associate
1 comment:
Try this to avoid some typing:
Function getChassisType(sstrComputer)
Set sobjWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& sstrComputer & "\root\cimv2")
Set objSystemEnclosure = sobjWMIService.Get _
("Win32_SystemEnclosure", 131072)
Set colChassis = objSystemEnclosure.Instances_()
intChassisType = -1
For Each objChassis in colChassis
intChassisType = objChassis.ChassisTypes(0)
Next
If intChassisType <> -1 Then
getChassisType = objSystemEnclosure.Properties_("ChassisTypes") _
.Qualifiers_("Values")(intChassisType - 1)
Else
getChassisType = "Some Error"
End If
Set sobjWMIService = Nothing
End Function
--
urkec
Post a Comment