Previous Section Table of Contents Next Section

Exploring WMI's Capabilities

Perhaps the easiest way to understand WMI is to simply start playing with it. Windows XP and Windows Server 2003 include Wbemtest.exe, a tool that can be used to test WMI functionality and explore its capabilities.

NOTE

Another acronym! WBEM stands for Web-Based Enterprise Management, Microsoft's implementation of several key DMTF technologies that includes WMI. You don't see the WBEM name as much as you used to, but it still pops up in tool names and the like.


To run Wbemtest, simply select Run from the Start menu, type wbemtest, and click OK. You'll see the main Wbemtest panel, shown in Figure 17.1.

Figure 17.1. The WMI Tester's main window

graphics/17fig01.gif

The first thing you need to do is connect to a WMI provider. Generally, that means connecting to the Windows Management Instrumentation service on your local machine or on another computer. I like to connect to the one on another computer, because it demonstrates WMI's real power as a remote administration tool. To connect, click the Connect button. You'll see the Connect dialog box, shown in Figure 17.2

Figure 17.2. Connecting to a remote machine's WMI namespace

graphics/17fig02.gif

To connect to a remote computer, type \\computername\root\cimv2. This instructs WMI to look for the specified computer name, connect to its root WMI namespace, and then switch to the cimv2 namespace. Cimv2 is simply the section that contains all of the Win32 classes, which are the ones you'll work with most often. Be sure to specify a user and password that has administrative privileges on the remote computer, because by default only administrators are allowed to work with WMI. Click Connect to make the connection.

After you're connected, click Enum Classes to force WMI to enumerate all available classes in the namespace. You'll be prompted for a superclass name; just leave it blank and click OK. You should see a dialog box like the one in Figure 17.3, listing all of the classes-both CIM and Win32 classes-that WMI found.

Figure 17.3. Enumerating the classes in the remote computer's cimv2 namespace

graphics/17fig03.gif

The next fun thing is to try querying. WMI supports a special query language called, appropriately enough, WMI Query Language, or WQL. It looks remarkably like SQL, and if you're familiar with writing SQL queries, WQL will look like an old friend. Start by clicking the Query button, and you'll see a dialog box like the one in Figure 17.4. Enter a query, such as SELECT graphics/ast.gif FROM Win32_NetworkAdapter Configuration. Be sure that WQL is selected for the query type, and click Apply. You'll see another dialog box, like the one in Figure 17.5. This dialog box lists all of the instances retrieved by your query. Remember, each instance represents, in this case, a single network adapter configuration. My computer, as you can see in Figure 17.5, has nine instances.

Figure 17.4. Writing a WMI query

graphics/17fig04.gif

Figure 17.5. Instances returned by the query

graphics/17fig05.gif

You can double-click any of the instances to display its information, as shown in Figure 17.6. This particular instance, as shown, has DHCP Enabled.

Figure 17.6. Examining an instance's properties

graphics/17fig06.gif

Hey, you've written your first WMI query! You may not even have noticed!

WQL Queries

Wbemtest is a great way to test WQL queries before including them in your scripts. You'll be able to immediately see what the query does, what information it returns, and so forth, which helps prevent errors in your scripts later on down the line.

WQL queries themselves are simple enough, and have five basic parts, one of which is optional.

  1. SELECT, which must start each WQL query.

  2. The properties you want to query. You can either provide a comma-separated list of property names, or if you want to retrieve all of a class' properties, specify graphics/ast.gif.

  3. FROM, which must follow the list of properties that you want to query.

  4. The name of the class you're querying.

  5. Optionally, you can include WHERE and a conditional statement. A WHERE clause limits the instances returned by your query. For example, if I include WHERE DHCPEnabled=TRUE in my earlier query, I receive fewer instances in the results, because only those instances of Win32_NetworkAdapterConfiguration that have DHCPEnabled set to True would be returned by the query.

Here are some sample WQL queries. If you like, try them in Wbemtest to see what they do!

  • SELECT * FROM Win32_NetworkAdapterConfiguration WHERE DHCPEnabled=TRUE

  • SELECT Description FROM Win32_Account WHERE Name='Administrator'

  • SELECT Freespace,DeviceID FROM Win32_LogicalDisk

Honestly, the best advice I can offer for quickly learning WMI is to explore the WMI class reference and start writing queries in Wbemtest. You'll quickly become familiar with WQL, and you'll see what type of information is returned by WMI. In the next two chapters, I'll focus on dealing with that information, especially complex information like IP addresses. Most importantly, do not be afraid to break something in Wbemtest. Even if you write the worst, malformed query known to mankind, the worst that can happen is Wbemtest will crash and you'll have to re-open it. No big deal, so experiment away!

    Previous Section Table of Contents Next Section