Downloads
39112.zip

The hardware inventory agent, HrdWrInv.vbs, relies on Windows Management Instrumentation (WMI) to retrieve hardware inventory. The agent relies on the FileSystemObject (which is part of the Scripting Runtime Library) to detect existing inventory files, create new inventory files, and write lines of data to each inventory file.

The script's first line of code contains the On Error Resume Next directive, which is important because you don't want to bother users with any operational errors that the script might generate. Such errors typically occur because the network access for creating the inventory files is configured improperly or because the WMI Core components haven't been installed or aren't operational on the system.

After defining some constants and global variables, the script's WMI connection string connects to the local computer and uses the WMI SWbemServices object's ExecQuery method to run a series of queries. Each query runs semisynchronously to improve script performance, and the script packages each query's result in an SWbemObjectSet collection. This collection contains the queried computer's hardware properties, which the script then writes to an inventory file. (If you haven't heard much about WMI, consider reading "Windows Management Instrumentation: The Journey Begins," July 2000, http://www.winnetmag.com, InstantDoc ID 8959. This article is a good primer and contains references to additional material. Also, visit the Microsoft Developer Network's—MSDN's—WMI Scripting Primer series, beginning at http://msdn.microsoft.com/library/en-us/dnclinic/html/scripting06112002.asp.)

The inventory agent uses the QueryInstances subroutine, which Listing A shows, to accomplish the bulk of its work. This subroutine takes three parameters: the name of the WMI class to query, the properties that the query should return, and any criteria for the query. For example, the following call to QueryInstances returns the properties in the strProperties variable for the Win32_LogicalDisk class (to collect logical disk information):

strProperties = _
"DriveType,Description," & _
"DeviceID,CreationClassName"
QueryInstances "Win32_LogicalDisk",strProperties, _
   "DriveType!=3 AND DriveType!=4"

Notice that the call to the subroutine specifies that the result set shouldn't contain DriveTypes 3 and 4, which represent a local disk and network drive, respectively. I don't collect local-disk information because I collect similar information from the Win32_DiskDrive class later in the script. I don't collect network drive information because it doesn't represent hardware on the local computer.

Other subroutines in the inventory script determine the unit measurements for properties that are stored using numeric values. For example, the size property of the Win32_DiskDrive class is stored as a byte measurement. The GetUnits function retrieves the unit measurement, then the SizeFormat function converts that measurement to a more readable value. (Showing that a disk drive is 17GB in size is probably more useful than showing that the drive size is 18,202,544,640 bytes.)