Previous Section Table of Contents Next Section

Determining What to Query

I mentioned in the previous chapter that actually figuring out which WMI class to query is the truly tough part about working with WMI, and it's true. The Microsoft MSDN Library reference to the WMI classes, particularly Microsoft's Win32 classes, is the most comprehensive and useful place to start looking. The documentation can be found online at http://msdn.microsoft.com/library. Just expand Setup and System Administration in the left-hand menu, then navigate to Windows Management Instrumentation, SDK Documentation, WMI Reference, WMI Classes, and Win32Classes.

Microsoft currently divides the classes into five categories, although that will almost certainly change over time as the classes are expanded. The current categories are

  • Computer System Hardware, which includes everything you can physically touch and see. This includes network adapters, the motherboard, ports, and so forth.

  • Operating System, which includes everything associated with Windows itself: users and groups, file quotas, security settings, COM settings, and more.

  • Installed Applications, which covers the Windows Installer subsystem and all managed applications.

  • WMI Service Management, which covers the configuration and management of WMI itself.

  • Performance Counter, which provides access to performance monitoring data through WMI.

After you've narrowed down the proper category, my best advice is to dive into the documentation and scroll through the class names until you find one that looks like it will do what you want. Need to force a hard drive to run CHKDSK? Hard drives are hardware, but CHKDSK is Windows-specific. After all, you don't really run CHKDSK on a hard drive, do you? You run it on a volume, which is a Windows thing. So, start with the Operating System category. There's the Win32_LogicalDisk category, which represents a volume like C: or D:. Lo and behold, it has a ChkDsk method and a ScheduleAutoChk method, one of which is sure to do the trick. The documentation also helpfully notes that the method is included only in Windows XP and Windows Server 2003, meaning that you'll have to find another way to handle earlier clients.

Microsoft's categorization of the WMI classes is far from consistent. For example, Win32_NetworkAdapterConfiguration is included in the Computer System Hardware category. Although I agree that a network adapter is definitely hardware, surely its actual configuration is part of the operating system, right? In other words, be prepared to do a little browsing to find the right classes, especially until you become accustomed to them.

TIP

The Appendix of this book is a Quick Script Reference I put together to help you locate the right WMI classes more quickly. For example, if you need to write a WMI query to retrieve Windows Product Activation information, just look up "Activation" in the Quick Script Reference. You'll see a reference to WMI, an indication that it's covered in Chapters 17 through 19, and the specific classes involved: Win32_WindowsProductActivation, for example.


No matter what, don't get discouraged. Keep browsing through the list until you find what you want. Just so you know, WMI isn't complete, yet. Microsoft hasn't provided a WMI "hook" for each thing Windows can do. In fact, the coverage for Windows XP and Windows Server 2003 is light-years better than what's in Windows 2000, and that's better still than NT. But even Windows Server 2003's implementation of WMI doesn't let you query or control the DHCP service, modify IPv6 in any way, modify DNS server records (although you can do that through ADSI in Active Directory-integrated DNS zones), or a hundred other tasks. Eventually, you'll probably be able to do all of those things with WMI, but not today.

Which Versions Include What?

The WMI documentation in MSDN Library is the most authoritative source for which WMI classes are included in which versions of WMI. Keep in mind that some classes gained new properties in newer versions of Windows. Near the end of each class' documentation page, you'll see something like the following:

Client: Included in Windows XP, Windows 2000 Professional, Windows NT Workstation 4.0 SP4, and later.

Server: Included in Windows Server 2003, Windows 2000 Server, Windows NT Server 4.0 SP4, and later.

Those are your official indications that the class (Win32_SystemTimeZone, in this example) is included in the listed versions of Windows. Take Win32_NetworkAdapter as a second example. The documentation indicates that it's available in Windows NT 4.0 SP4 and later, but check out the InterfaceIndex property. That property includes a note:

Windows XP and earlier: The InterfaceIndex property is not available.

Meaning, of course, that the property was introduced in Windows Server 2003. This particular property had to be added to Win32_NetworkAdapter when Microsoft added the Win32_IP4RouteTable class, because the route table class needed some unique number with which it could refer to network adapters. As WMI continues to grow with new classes, supporting properties will be added to existing classes to make the package complete.

Suppose you want to work with disk quotas on your Windows 2000 file servers. You find a great-looking class, Win32_DiskQuota, but your scripts don't seem to have any effect on your Windows 2000 machines. That's because, as the documentation notes, the class was introduced in Windows XP. How can you retrofit it to 2000? You can't. Unfair, but that's progress.


    Previous Section Table of Contents Next Section