WMI (Windows Management Instrumentation)
A WMI Script, which Employs Select Case to Discover Chassis Type
The practical point of this WMI script is to discover if the computer is a Mini
Tower, Desktop, Laptop or a Rack Mounted unit. While this is a fairly trivial task, it frees up our minds to concentrate on the pure VBScript commands. View this script as a vehicle to dissect and experiment with 'Select Case' and InputBox().
WMI Topics
Example - Discover your Computer's Type or 'Chassis Form' WMI Tutorial - Learning Points
Summary - WMI Memory
Example
-Discover your Computer's Type or 'Chassis Form'
This example employs WMI to interrogate the operating system for information on the computer's chassis type. The VBScript portion of the script neatlydemonstrates the Select Case statement.
Prerequisites for your WMI Script
No specific requirements. This script should display the type or 'Form' of any computer.
Instructions for Creating your 'Chassis Form' WMI Script
1. Copy and paste the example script below into notepad or a VBScript editor.
2. Save the file with a .vbs extension, for example: ComputerChassis.vbs 3. Double click ComputerChassis.vbs and check the Computer's Chassis
Introduction to VBScript.doc By Guy Thomas
Page 55 of 59
Script to Discover your Computer's type or 'Chassis Form'
' ComputerChassis.vbs' VBScript to discover the computer type or 'Chassis Form' ' Author Guy Thomas http://computerperformance.co.uk/ ' Version 1.4 - September 2005
' ---' Option Explicit
Dim objWMIService, objComputer, objChassis, objItem Dim strComputer, strChassis, colChassis
strComputer = "."
strComputer = InputBox("Enter Computer name", _ "Find Computer Type", 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 "Your computer type is: " & strChassis WScript.Quit
WMI Tutorial
-Learning Points
From a VBScript perspectiveNote 1: I am very fond of the, 'If.. Then...Else' construction, however it
becomes unwieldy after about the third 'else'. Then, one day, I discovered the superior Select Case construction. Straightaway, I knew that Select Case would be my salvation for scripts that dealt with multiple choices. My only difficulty is remembering the correct sequence of those two words: Select Case.
Introduction to VBScript.doc By Guy Thomas
Page 56 of 59 From a WMI perspective
Note 2: We need to connect to the Root of the CIM namespace. The first procedure tells winmgmts to access the root of the CIM library :
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\ "
& strComputer & "\ root\ cimv2")
Note 3: In most WMI examples, we need security clearance to query a machine's hardware, this is why we add :
& "{impersonationLevel=impersonate}!\\ " _
To begin with, just trust me and accept that WMI scripts usually need this command to be sure that you have the right to query the operating system.
Note 4: Set colComputer = objWMIService.ExecQuery _ Here is a standard VBScript phrase to prepare for the WQL command: Select * from
Win32_SystemEnclosure". The part of Win32 that we are particularly interested in is SystemEnclosure. Win32 has dozens of possible properties, here we need to query the SystemEnclosure collation of properties.
Summary
-WMI
This WMI example shows you how to interrogate the operating system and
discover information about the computer's Chassis type. As you acquire VBScript skill, so you can tune up your scripts with an inputbox. Once you have mastered one Win32 object then you can apply the method to investigate objects such as Win32_PhysicalDisk or Win32_Processor.
This page covered the following VBScript methods, commands and syntax:
Introduction to VBScript.doc By Guy Thomas
Page 57 of 59
Index for Introduction to VBScript
Who is this Introduction to VBScript ebook aimed at? ... 1
Introduction to VBScript
by Guy Thomas
... 2
Simple, yet powerful VBScripts ... 2
Introduction to VBScript ... 2
Tasks for VBScript... 2
VBScript Methods, Commands & Syntax... 2
Section 0 Introduction to VBScript... 3
Introduction - What are the Benefits of Learning VBscript?... 3
Skills (Multi-Skills) ... 3
Secrets of Scripting ... 4
Section 1
VBScript - Check Version ... 5
Introduction to Checking Version Numbers ... 5
Topics for Checking Version Numbers... 5
Example 1 - VBScript to Check the WSH Version... 5
Prerequisites ... 5
Sample VBScript to Check the WSH Version ... 6
Learning Points ... 6
Example 2 - VBScript to Check the WSH Version and Build Number ... 7
Learning Points ... 7
Example 3 - VBScript to Check the VBScript Version ... 7
Sample Script to Check the VBScript Version ... 8
Learning Points ... 8
Challenge: ... 8
Summary of Checking Version Numbers ... 8
Section 2
VBScript - Variables... 9
Introduction to VBScript Variables... 9
Topics for Variables... 9
What is a VBScript Variable?... 9
Example 1 - Using a String Variable ... 10
Learning Points ... 10
Example 2 - Using a Variable to Increment a Loop. ... 11
The Hungarian Variable Convention ... 12
Hungarian Convention Example: ... 13
Learning Points ... 13
Summary of VBScript Variables ... 13
Section 3
Create an OU (Organizational Unit) in Active Directory... 14
Tutorial for Creating an OU (Organizational Unit) with VBScript ... 14
Topics for Creating an OU (Organizational Unit) with VBScript ... 14
Our Mission and Goal... 14
Example 1 - Create a Top Level OU ... 14
Learning Points - Binding to Active Directory ... 16
Learning Points - Creating the actual OU ... 17
Example 2 - Create a Child OU... 18
Learning Points - Creating the Child OU ... 19
Example 3 - Adding error-correcting Code ... 19
Summary Creating an OU ... 20
Section 4
Create a User in Active Directory ... 21
Introduction to Create a User in Active Directory... 21
Tutorial for Creating a User Account with VBScript... 21
Topics for Creating a User Account with VBScript ... 21
Our Mission and Goal... 21
Example 1 - Script to Create a User in Active Directory ... 22
Prerequisites ... 22
Script to Create a User in a Named OU (Organizational Unit) ... 22
VBScript Tutorial - Learning Points ... 23
Introduction to VBScript.doc By Guy Thomas
Page 58 of 59
VBScript Tutorial - Learning Points ... 24
Summary of Creating User Accounts ... 25
Section 5
Map Network Drive ... 26
Introduction to MapNetworkDrive... 26
Topics for MapNetworkDrive... 26
Our Mission and Goal... 26
Example - MapNetworkDrive VBScript Method ... 27
Learning Points for MapNetworkDrive... 27
Guy's Challenges ... 28
Summary of MapNetworkDrive ... 28
Section 6
Map a Network Printer... 29
Introduction to AddWindowsPrinterConnection Logon Script ... 29
Topics for AddWindowsPrinterConnection Scripts ... 29
Our Mission ... 29
Example 1 - Simple Printer Script with AddWindowsPrinterConnection... 29
Learning Points... 30
Example 2 - Printer Script with AddWindowsPrinterConnection ... 31
Example 2 - Printer Script with AddWindowsPrinterConnection ... 31
Learning Points... 31
Example 3 - SetDefaultPrinter... 31
Learning Points... 32
Summary of AddWindowsPrinterConnection ... 32
Section 7
Create a Folder (Stage 1 of 3)... 33
Introduction to Create Folders... 33
Topics for VBScript... 33
Meet the VBScript terms ... 33
Example 1 - Basic VBScript to Create a Folder ... 34
Prerequisites ... 34
Sample Script to Create a Folder ... 34
VBScript Tutorial - Learning Points ... 35
Example 2 - Create a Folder with error-correcting Code ... 35
Example 3 - Script to Create a Folder, then Open Windows Explorer ... 35
VBScript Tutorial - Learning Points ... 36
Summary of Creating Folders ... 36
Section 8
Create a Text File (Stage 2 of 3)... 37
A VBScript Tutorial for Creating a File ... 37
Topics for Writing or Appending to a File with VBScript ... 37
Our Mission and Goal... 37
Example 1 - Basic VBScript to Create a File with CreateTextFile ... 37
Prerequisites ... 37
Sample Script to Create a File ... 38
VBScript Tutorial - Learning Points ... 38
Example 2 - Create a File with error-correcting Code... 39
Sample Script to Create a File and Check if a File Already Exists ... 39
VBScript Tutorial - Learning Points ... 40
Summary of Creating Files... 40
Section 9
Append Data to Text File (Stage 3 of 3)... 41
A VBScript Tutorial for Writing or Appending to a File ... 41
Topics for Writing or Appending to a File with VBScript ... 41
Our Mission and Goal... 41
Example - VBScript to Append Text to a File ... 41
Prerequisites ... 42
Sample Script to Append Text to a File... 43
VBScript Tutorial - Learning Points ... 44
Summary of How to Append Data to a Text File ... 44
Section 10
Registry Scripting ... 45
Introduction Registry Scripting... 45
Topics for Registry Scripting ... 45
Introduction to VBScript.doc By Guy Thomas
Page 59 of 59
Example 1 - To Extract Windows Version Information from the Registry ... 45
Learning Points... 46
Challenges ... 47
Summary - Reading the Registry ... 47
Mission to Write Keys to the Registry ... 47
Example 2 - To Write Information into the Registry ... 47
Learning Points... 48
Challenges ... 49
Summary - Scripting the Registry ... 49
Section 11
WMI Scripting - Memory ... 50
Introduction to WMI ... 50
WMI Topics ... 50
Example 1 - Discover your Computer's Memory... 50
Prerequisites for your WMI Script ... 50
Script to Discover how much Memory (RAM) is in your computer ... 51
WMI Tutorial - Learning Points... 51
Example 2 - WMI Memory Script with an Input Box... 52
WMI Tutorial - Learning Points... 53
Summary - WMI Memory ... 53
Section 12
WMI Scripting - Chassis Type ... 54
A WMI Script, which Employs Select Case to Discover Chassis Type ... 54
WMI Topics ... 54
Example - Discover your Computer's Type or 'Chassis Form'... 54
Prerequisites for your WMI Script ... 54
Script to Discover your Computer's type or 'Chassis Form' ... 55
WMI Tutorial - Learning Points... 55
Summary - WMI ... 56