What is Windows Management Instrumentation(WMI)?

WMI provides a model to represent, store and query configuration and status information of computers running Windows operating system. You can use WMI to fetch information from your system or from any other system in your network.

Where can we use WMI in QTP?

You can use WMI to query and retrieve information from a computer where a QTP script is running. WMI can be used for tasks like – but not limited to – finding the time zone of a machine, retrieving information about the currently running processes that you see in Windows Task Manager (Ctrl – Shift – Esc), finding physical memory (RAM), current RAM (commit charge), CPU usage etc.

Here is a sample script to find the time zone of a machine.

 

Dim oWMIService, oComputer, colComputer
Dim strComputer

strComputer = "."

Set oWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")

Set colComputer = oWMIService.ExecQuery _
("Select * from Win32_TimeZone")

For Each oComputer in colComputer

msgbox oComputer.Description

Exit For
Next

Set oComputer = Nothing
Set colComputer = Nothing
Set oWMIService = Nothing

 

Let us see what the statements above mean:

  • GetObject is a VBScript function that returns a reference to an object provided by a COM component. [COM stands for Component Object Model technology that allows various software components –under Windows- to interact with each other. QTP’s Automation Object Model, Excel automation model comes under the broad heading of COM]
  • winmgmts: – is a WMI Scripting Library’s moniker. This is a mandatory part to be supplied to the string inside GetObject function.
  • _ : Underscore. This is a syntax of VBscript, used when VBScript statements flows to another line.
  • impersonationLevel=impersonate: It is one of the optional security setting components of WMI. As the dictionary meaning suggests, impersonationLevel=impersonate ‘pretends’ that action is being carried by a user instead of a script. It is especially useful while accessing information about remote computers.
  • ! – Exclamation Mark. Impersonation levels can be used to enable/disable certain privileges. ! is used as a symbol for negation. In the above case it would disable the default privileges associated with the default impersonationLevel.
  • strcomputer: is a variable that holds the path of computer for which you are trying to retrieve information. Dot (.) signifies the local machine. (default path)
  • rootcimv2: cimv2 is a namespace under root namespace. We will talk about namespaces in a while.
  • ExecQuery (“Select * from Win32_TimeZone”): This executes the query and return information from the Win32_TimeZone class to a collection.
  • oComputer.Description: Description is one of the properties under Win32_Timezone class. All properties of a given class can be found in MSDN. For ex: Here is a list of properties for Win32_Timezone class

Output for the script above would look something like this:

QTP Time Zone WMI

You can copy the above code into a .vbs file to get the time zone of your machine.

What do we mean by namespace?

Namespace in WMI can be defined as a logical database of classes and their instances. The parent of all namespaces is the root. Here is a sample script to get all namespaces contained inside root.

Set objWMIService = GetObject("winmgmts:\.root")
Set colNamespaces = objWMIService.InstancesOf("__NAMESPACE")

For Each objNamespace In colNamespaces
print objNamespace.Name
Next

Namespaces under root

rootCIMV2 is the default WMI namespace. So, the first script would work fine even with this statement:

Set oWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\”)

Which namespace is required for us?

As QTP developer, we would be working with CIMV2 workspace. So, we can consider this part to remain the same while writing WMI scripts.

Set oWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\" _ & strComputer & "rootcimv2")

How to write scripts in WMI?

  1. Download WMI Code Creator. A tool created by Microsoft to write scripts in WMI.
  2. Once downloaded, unzip the file and run the .exe file. No installation is required.
  3. This is what WMI Code Creator would look like. The first dropdown contains the list of namespaces while the second drop down consists of the corresponding WMI classes.wmi-code-generator
  4. WMI Code Creator allows you to create scripts in VBScript, Visual Basic .Net, C# by selecting ‘Code Language’ from the menu bar at the top.

How to identify which class and which property we want?

There is no straightforward answer for this question. Knowledge about classes and corresponding properties comes with experience. Having said that, the names assigned to various classes are quite meaningful. One way is to think of what name you would assign to a given class if you were the developer.
Also, in all probability what you intend to develop may be already present on the web. It may be just a matter of using the search engine of your choice. Once you get the class name, things get easy from there.

Here are some commonly used classes while working on WMI with QTP:

  • Win32_Process – This class represents information about the processes running on a windows machine.
  • Win32_TimeZone – This class represents the time zone information for a computer system running windows.
  • Win32_Processor – This class represents information about the machine’s processor. On a multiprocessor computer, one instance of the Win32_Processor class exists for each processor.

References:

  1. Wikipedia
  2. Windows Networking