Active TopicsActive Topics  Display List of Forum MembersMemberlist  CalendarCalendar  Search The ForumSearch  HelpHelp
  RegisterRegister  LoginLogin


 One Stop Testing ForumTesting Tools @ OneStopTestingQuickTest Pro @ OneStopTesting

Message Icon Topic: All About Windows Management Instrumentation (WMI)

Post Reply Post New Topic
Author Message
Mithi25
Senior Member
Senior Member
Avatar

Joined: 23Jun2009
Online Status: Offline
Posts: 288
Quote Mithi25 Replybullet Topic: All About Windows Management Instrumentation (WMI)
    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. 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)
  • \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 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 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.




Post Resume: Click here to Upload your Resume & Apply for Jobs

IP IP Logged
Post Reply Post New Topic
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum



This page was generated in 0.125 seconds.
Vyom is an ISO 9001:2000 Certified Organization

© Vyom Technosoft Pvt. Ltd. All Rights Reserved.

Privacy Policy | Terms and Conditions
Job Interview Questions | Placement Papers | Free SMS | Freshers Jobs | MBA Forum | Learn SAP | Web Hosting