Introduction
Web Based Enterprise Mangaement (WBEM) is an industry initiative that establishes management infrastructure standards
and provides a way to combine information from various hardware and software management systems. It specifies standards
for a unified architecture allowing access to data from multiple underlying technologies and platforms and presenting
it in a consistent fashion.
Microsoft Windows Management Instrumentation (WMI) is WBEM-compliant. It provides a consistent and descriptive model
of the configuration, status and operational aspects of Microsoft Windows.
In essence, WMI provides:
- Consitent model of Windows configuration, operation and status
- A COM API providing a single point access to all management functions
- Flexible architecture allowing vendors to extend the information model to cover new devices,
applications, and other enhancements by writing WMI providers
- WMI Query Language, WQL, that enables detailed queries of the information model
- Scripting support, through Visual Basic, VBA, VBScript and JScript
In this article, we take the route of VBScript to discover some possibilities in WMI.
What do we do?
Let's create a simple VBScript to query WMI regarding the disks.
Code and Analysis
The first method that would ever used is to get the reference to the COM Object - winmgmts (CLSID: 172BDDF8-CEEA-11D1-8B05-00600806D9B6).
This is the "Wbem Scripting Object Path" / "Wbem Object Path 1.0" Component. The complete code is:
GetObject("winmgmts:\\{machine-name}\root\cimv2")
In the code above, {machine-name} refers to the machine where the query is to be performed subsequently. Should you wish to perform the
query on local machine, you can specify the name as "." (dot).
Now, I mentioned about WQL earlier. It's very similar to SQL, if you know that. To get started, it's identical. Let's query our machine
to some basic information.
1 Option Explicit
2 Dim objWMIService, objItem, colItems, strComputer
3
4 strComputer = "."
5
6 Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
7 Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")
8
9 For Each objItem in colItems
10 Wscript.Echo _
11 "Name: " & objItem.Name & vbCr & _
12 "System Type: " & objItem.SystemType & vbCr & _
13 "Manufacturer: " & objItem.Manufacturer & vbCr & _
14 "Model: " & objItem.Model & vbCr & _
15 "# Processors: " & objItem.NumberOfProcessors & vbCr & _
16 "RAM: " & CInt(objItem.TotalPhysicalMemory / (1024 * 1024)) & " MB" & vbCr & _
17 "Username: " & objItem.UserName & vbCr & _
18 "Timezone: " & objItem.CurrentTimeZone & " mins from GMT" & vbCr
19 Next
20
21 WSCript.Quit
At line 6, we get a reference to the COM Object for the root CIM v2 for the machine. CIMv2 stands for Common Information Model Version 2.
At line 7, we query the service for the class Win32_ComputerSystem.
Subsequently, we iterate through all the items and extract the information. In this case, the loop iterates only once because
there is only one Computer System.
And the result is something as below:
Now, how about querying the information about the disk drive?
1 Option Explicit
2 Dim objWMIService, objItem, colItems, strComputer, intDrive
3
4 strComputer = "."
5
6 Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
7 Set colItems = objWMIService.ExecQuery("Select * from Win32_DiskDrive")
8
9 For Each objItem in colItems
10 intDrive = intDrive + 1
11 Wscript.Echo _
12 "DiskDrive: " & intDrive & vbCr & _
13 "Caption: " & objItem.Caption & VbCr & _
14 "Description: " & objItem.Description & VbCr & _
15 "Manufacturer: " & objItem.Manufacturer & VbCr & _
16 "Model: " & objItem.Model & VbCr & _
17 "Name: " & objItem.Name & VbCr & _
18 "Partitions: " & objItem.Partitions & VbCr & _
19 "Size: " & CInt(objItem.Size / (1024 * 1024 * 1024)) & " GB" & VbCr & _
20 "SystemName: " & objItem.SystemName & VbCr
21 Next
22
23 WSCript.Quit
I don't think I need to explain the code above. Everything should be self-explanatory. The result is similar to the following:
Conclusion
WMI provides a great way to manipulate all system information, including software and hardware.
What more - WMI Scripting provides a simple yet powerful mechanism for the same.