Instructions for Creating a WMI script to Discover the Chassis Type
- Logon as a local Administrator.
- Copy and paste the example script below into notepad or use a VBScript editor.
- Save the file with a .vbs extension, for example: Chassis.vbs
- Double click and check machine's chassis type.
- Optional suggestion edit strComputer = "." to the NetBIOS name of another computer.
کد:
' Chassis.vbs
' VBScript to interogate a machine's chassis type.
' Author Guy Thomas http://computerperformance.co.uk/
' Version 2.3 - November 2005
' ---------------------------------------------------------'
Option Explicit
Dim strComputer, strChassis
Dim objWMIService, objChassis, colChassis, objItem
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colChassis = objWMIService.ExecQuery _
("Select * from Win32_SystemEnclosure",,16)
For Each objChassis in colChassis
For Each objItem in objChassis.ChassisTypes
Select Case objItem
Case 1 strChassis = "Maybe Virtual Machine"
Case 2 strChassis = "??"
Case 3 strChassis = "Desktop"
Case 4 strChassis = "Thin Desktop"
Case 5 strChassis = "Pizza Box"
Case 6 strChassis = "Mini Tower"
Case 7 strChassis = "Full Tower"
Case 8 strChassis = "Portable"
Case 9 strChassis = "Laptop"
Case 10 strChassis = "Notebook"
Case 11 strChassis = "Hand Held"
Case 12 strChassis = "Docking Station"
Case 13 strChassis = "All in One"
Case 14 strChassis = "Sub Notebook"
Case 15 strChassis = "Space-Saving"
Case 16 strChassis = "Lunch Box"
Case 17 strChassis = "Main System Chassis"
Case 18 strChassis = "Lunch Box"
Case 19 strChassis = "SubChassis"
Case 20 strChassis = "Bus Expansion Chassis"
Case 21 strChassis = "Peripheral Chassis"
Case 22 strChassis = "Storage Chassis"
Case 23 strChassis = "Rack Mount Unit"
Case 24 strChassis = "Sealed-Case PC"
End Select
Next
Next
WScript.Echo "Computer chassis type: " & strChassis
'WScript.Echo strComputer & "'s chassis type: " & strChassis
WScript.Quit
' End of WMI VBScript - Chassis type
Learning Points
Note 1: Consider this section of the script.
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
The above phrase is standard in so many WMI scripts. What it says in plain English is, connect to the heart of the WMI namespace. {impersonationLevel=impersonate} is one of those commands that I just accept is necessary. In fact, it reminds me to logon as an administrator so that the script will not fail for the trivial reason of insufficient user rights.
Note 2: Set colChassis = objWMIService.ExecQuery _
("Select * from Win32_SystemEnclosure",,16)
As ever, note the verbs. Firstly, ExecQuery which says carry out the request. Select * is famous in all query languages, get all.
Note 3: Identify the class - Win32_SystemEnclosure. The Win32_xyz class is the first place that I look to see the purpose of a particular script. SystemEnclosure means the computer 'Box', 'Unit', the chassis type.
Note 4: This script employs two loops. The phrase 'col' means collation. WMI scripts interrogate a whole collection of properties, for this example I have singled out objChassis.ChassisTypes.
Note 5: I never miss an opportunity to showcase the 'Select Case' construction. Multiple 'Else If' would be ungainly, whereas Select Case works elegantly.
Note 6: Edit strComputer = "." and substitute the name of another machine on your network. If you accept this challenge, uncomment the last but 3 line.
Summary of WMI
Remember that the operating system knows everything. Thank Microsoft for providing a library of WMI commands with which to interrogate systems like the Event Logs. WMI scripting is tricky because there are so many elements. Take the time to have a walk through of how you examine an Event Log property sheet. In particular, match the Event ID, Success or Failure and type of Log with variables in my script.