Print Page | Close Window

All About Windows Management Instrumentation (WMI)

Printed From: One Stop Testing
Category: Testing Tools @ OneStopTesting
Forum Name: QuickTest Pro @ OneStopTesting
Forum Discription: QuickTest Pro is a fresh approach to automated software and application testing that addresses testing needs of both business analysts and Quality Assurance professionals.
URL: http://forum.onestoptesting.com/forum_posts.asp?TID=7033
Printed Date: 30May2024 at 2:34pm


Topic: All About Windows Management Instrumentation (WMI)
Posted By: Mithi25
Subject: All About Windows Management Instrumentation (WMI)
Date Posted: 22Sep2009 at 12:52am

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.

01.Dim oWMIService, oComputer, colComputer
02.Dim strComputer
03. 
04.strComputer = "."
05. 
06.Set oWMIService = GetObject("winmgmts:" _
07.& "{impersonationLevel=impersonate}!\\" _
08.& strComputer & "\root\cimv2")
09. 
10.Set colComputer = oWMIService.ExecQuery _
11.("Select * from Win32_TimeZone")
12. 
13.For Each oComputer in colComputer
14. 
15.msgbox oComputer.Description
16. 
17.Exit For
18.Next
19. 
20.Set oComputer = Nothing
21.Set colComputer = Nothing
22.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. http://mercuryquicktestprofessional.blogspot.com/2008/05/qtp-aom-and-how-to-write-scripts-in-aom.html - QTP’s Automation Object Model , http://www.learnqtp.com/use-test-data-directly-from-external-sheet/ - 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 http://www.microsoft.com/technet/scriptcenter/guide/sas_wmi_vzbp.mspx?mfr=true - security setting components of WMI. As the http://dictionary.reference.com/browse/impersonate - 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)
  • \root\cimv2: 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 http://msdn.microsoft.com/en-us/library/aa394498%28VS.85%29.aspx - 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.

1.Set objWMIService = GetObject("winmgmts:\\.\root")
2.Set colNamespaces = objWMIService.InstancesOf("__NAMESPACE")
3. 
4.For Each objNamespace In colNamespaces
5.print objNamespace.Name
6.Next

Namespaces under root

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

1.Set oWMIService = GetObject("winmgmts:" _
2.& "{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.

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

How to write scripts in WMI?

  1. Download http://www.microsoft.com/downloads/details.aspx?FamilyID=09dfc342-648b-4119-b7eb-783b0f7d1178&DisplayLang=en - Scriptomatic . A tool created by Microsoft to write scripts in WMI.
  2. Once downloaded, unzip the file and run the file with .hta extension. No installation is required.
  3. This is what Scriptomatic would look like. The first dropdown contains the list of namespaces while the second drop down consists of the corresponding WMI classes. scriptomatic-qtp
  4. Scriptomatic allows you to create scripts in VBScript, Perl, JScript and Python at a click of a radio button.

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.



-------------
http://www.quick2sms.com - Send Unlimited FREE SMS to Any Mobile Anywhere in INDIA,
Click Here



Print Page | Close Window