Last month, I discussed EventMon.pl, a script that monitors events in logs on remote Win32 machines and displays those events in realtime. The magic behind this script is Windows Management Instrumentation (WMI). In my explanation of EventMon.pl, I mentioned that you ought to become familiar with WMI. This month, I insist on it.
Many of you are probably rolling your eyes right now and moaning about yet another Microsoft technology to learn. WMI is well worth learning because it offers a rich set of features that will meet most of your administrative needs. With WMI, your Perl scripts can query a machine's settings, processes, hardware, and more. These scripts can even monitor events that a remote machine fires.
To help you learn WMI, I explain how to access WMI from Perl. In addition, I provide a practical example of how you can use WMI with Perl. I don't focus on the nitty-gritty but rather give a cursory overview of the information you need to start using this technology.
Understanding WMI
WMI is Microsoft's version of an industrywide initiative called Web-Based Enterprise Management (WBEM). This initiative uses the Common Information Model (CIM), which describes how information is obtained.
Classes are key to WMI. Think of a class as a blueprint. Architects create blueprints to inform others about a building's design and characteristics. For example, a house's blueprint tells you the size of the rooms, the number of fireplaces, and other pertinent information. Builders use that blueprint to create a physical manifestation of the house.
Similarly, code writers create classes to inform others about objects' properties and functions (also called methods). In the case of WMI, Microsoft code writers created WMI classes to describe objects that represent computer components. For example, the Win32_DiskDrive class describes the object that represents a machine's physical disk drive. This class tells you the size of the disk drive, the number of heads, and other pertinent data. Perl scripts use that class to create a physical manifestation, or instance, of the object in the computer's memory. You can then examine and explore the object, just as you might examine and explore a new house. WMI classes exist for all types of information. Classes address a computer's physical characteristics (e.g., memory), OS (e.g., available virtual memory), applications (e.g., installed programs), subsystems (COM components), and more.
Using WMI Classes
To request that a machine create an instance of, or instantiate, a class into an object, you need to know which class to request. You can access WMI Win32 class information from the Microsoft Developer Network (MSDN) Online Library. After you know which class you want to use, you can instantiate that class by following these steps:
- Load the Win32::OLE extension.
- Request an instance of the object that represents the WMI service on the target machine.
- Request all instances of the target class.
- Enumerate each instance in the target class.
For example, let's use WMI to request information about a machine's disk drives, which means you need to instantiate the Win32_DiskDrive class.
Step 1
The first step is to load the Win32::OLE extension. This extension's name is misleading because Win32::OLE doesn't have much to do with OLE; instead, the extension has more to do with COM.
The Win32::OLE extension comes with ActivePerl, so you probably already have the extension installed. (If you don't have ActivePerl, you can download it from ActiveState Tool at http://www.activestate.com.) If you're unfamiliar with the Win32::OLE extension, I recommend you learn about it because it's one of the most useful Win32 extensions.
To load the extension, you use the code that Listing 1 shows for Step 1. In addition to loading the extension, this code imports the in() function so that you can use it later in your script (more on this function later).