Published: Wednesday, August 28, 2002
Using Windows Management Instrumentation (WMI) in an ASP Page
By Bill Wooten
Introduction
Recently I had the task of gathering system information from various remote machines and displaying
that information on a Web page. Initially I didn't know what tools and technologies I had it my disposal
for providing such information in a Web page interface, but after a little research I found the
Windows Management Instrumentation (WMI) SDK. Essentially, WMI allows users to gather information on memory,
active directory (2000 only), installed applications, services, processes, and ODBC.
Additionally you can manage remote clients: reboot, registry manipulation, event logs, security,
services, processes, and network management. Best of all the WMI SDK can be accessed through a
scripting interface, such as VBScript in an ASP page or through the Windows Scripting Host.
To use the WMI SDK to gather information or perform system administrator-type operations on remote computers
those remote computers must have the WMI run time installed. Fortunately the WMI SDK and run time
are freely downloadable from Microsoft's Web site at:
http://msdn.microsoft.com/downloads/default.asp?URL=/code/sample.asp?url=/msdn-files/027/001/566/msdncompositedoc.xml
(Note that the SDK includes everything you'll need, both the SDK and the run time for client machines...)
Using WMI in an ASP Page
There are a number of practical uses in which WMI could be used through an ASP page.
For example, one could use WMI to gather and inventory system information across the network and store
it in a database. One might want to monitor remote client performance (memory/processor usage), or
inventory application information remotely (browser version, etc). If you've only used ASP pages
to create Internet sites, it may seem a bit odd to consider using ASP for such tasks, but those who
have used ASP primarily for intranet development will likely see the utility in using WMI and ASP in this
fashion.
Of course one could use a simple Windows Hosting Script (WSH) script to accomplish this, leaving ASP out
of the equation. However, if you want to be able to view the status of your network remotely, an ASP
page that gathers such information and provides it on an Internet page can be quite useful. For example,
check out this live demo I've setup on
Brinkster, which shows the processor status for the computer the ASP script is executing on (note that
the machine has two processors).
If you download the WMI SDK you'll find that there are a number of ASP examples in the SDK's documentation.
Regardless, I think it would be helpful to show a quick example here myself. WMI can make calls to
various "classes" of system information. For example, there's a Win32_Process
class, and a Win32_diskdrive class. These classes each have a listing of properties.
My example is simply the creation of a function that opens an instance of the specified class and
outputs an HTML table listing all of the property names and values. The live demo
essentially uses this function that I am about to present, specifying that it should display the
values of the Win32_Process's properties.
The function, ShowServices, takes two input parameters: vComputerName, the
name of the computer whose information should be displayed; and vClass, the name of the
WMI class whose properties we're interested in displaying.
Function ShowServices(vComputerName, vClass)
'Declare our needed variables...
Dim objLocator, objService, objWEBMCol, objWEBM, objProp, propitem, objItem
|
The first step in using WMI is to create the WMI Locator Object (SWbemLocator).
This object can return a services
object for a particular computer. This services object can then be used to get information about
a particular WMI class. Hence, the first step we need to perform is to create an instance of our locator
object and then connect to the specified computer, which can be done with the following lines of code:
Set objLocator = CreateObject("WbemScripting.SWbemLocator")
Set objService = objLocator.ConnectServer(vComputerName)
|
Note that the ConnectServer
method returns an instance of the services object (which we store in objService). Furthermore,
realize that the ConnectServer method has a number of optional parameters, such as namespace, user, password, authority, etc. More information
about these parameters can be found by reading the technical
documentation. For our purposes we'll keep it simple and just omit the parameters, thereby using
the default values.
Now that we have our services object we can use the object's Get
method to retrieve an instance of the WMI class we're interested in, like so:
Set objWEBM = objService.get(vclass)
|
Note that more than one class object may be returned by the Get method. For example, if we're
retrieving information about the machine's processors, if the machine has multiple processors then
more than once class instance will be returned by Get. Specifically, Get
returns a collection of class objects. In order to work with this collection, we simple reference the
Instances_ property of the objWEMB object:
Set objWEBMCol = objWEBM.Instances_
|
The objWEBM object also contains a collection of all of the properties for the specified class,
which is stored in the Properties_ property. As with the collection of class instances,
let's store this collection of properties in a local object:
Set objProp = objWebm.properties_
|
Recall that the purpose of this function is to display an HTML table with all of the property values
for each class instance. After emitting the initial HTML table markup (omitted here for brevity), we
need to emit the name of each of the properties in the table's column headings. This can be done by
simply looping through the objProp collection:
For Each propItem in objProp
Response.Write "<th>" & propItem.name & "</th>"
Next
|
At this point we're almost done: we simply need to iterate through each class of the class in the
objWEBMCol collection, outputting each property for each class instance:
For Each objItem in objWEBMCol
Response.Write "<tr>" & chr(13)
For Each propItem in objProp
Response.Write "<td>"
Response.Write Eval("objItem." & propItem.Name)
Response.Write "</td>" & chr(13)
Next
Response.Write "</tr>"&chr(13)
Next
|
Note that when outputting the WMI class's property value I had to use the Eval function.
Since we are allowing the developer to specify the name of the class to get at run time, there's no
way that I can know the specific property names that will be included in the class. Hence, I loop
over the collection of property names (objProp), and, for each property, I use Eval
to grab the current item's property value. (To learn more about VBScript's Eval function
be sure to read: Using the Eval and Execute
Functions!)
This concludes the germane code portions of the ShowServices function (you can also
view the entire code for the function). To use this function
to emit an HTML table you simply need to call the function and pass it the required parameters.
The live demo calls this function
for the current machine (using "." as the first parameter), gathering information about
the Win32_Processor class. That is, the function is called as follows:
Call ShowServices(".", "Win32_Processor")
|
Conclusion
In this article we examined what WMI is and its usefulness. We looked at how to use WMI through an ASP
page and how to write a function that would take a machine name and WMI class as inputs and emit an
HTML table as output. To get started with WMI be sure to
download
the WMI SDK from Microsoft.
Happy Programming!
By Bill Wooten
Attachments
Download
the WMI SDK
View a live demo!
Download the complete source code to the ShowServices function (in text format)