Taking Inventory of Computer Hardware

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

Although different hardware settings must be accessed through different WMI classes, the scripting approach is identical in each case:

  1. Your script connects to the WMI service on the computer to be inventoried.
  2. Your script connects to the appropriate WMI class.

    For example, if you want a list of CD-ROM drives, you should connect to the Win32_CDROMDrive class. If you want information about display adapters, you should connect to the Win32_VideoController class.

  3. For each item in the returned collection (that is, each installed CD-ROM drive or each installed display adapter), retrieve the desired properties.

One nice feature of WMI is the fact that no error condition occurs if you attempt to enumerate hardware that does not exist. For example, suppose you are trying to inventory tape drives, and no tape drive exists on a particular computer. This is not a problem; you will still receive a collection of the tape drives on that computer. The only difference is that the collection will have no items in it.

Of course, you do not receive any "official" notice that the computer does not have any tape drives; you have to infer this from the fact that no tape drive information was returned. To help verify that the computer does not have any tape drives, you can use the Count property to check the number of items in the collection. If the Count is 0, you can echo the message, No tape drives were found in this computer. If the Count is greater than 0, you can then report the property values for each tape drive.

The following code sample uses the Count property to identify zero-item collections:

If colTapeDrives.Count = 0 Then     Wscript.Echo "No tape drives were found in this computer." Else     For Each objTapeDrive in colTapeDrives         Wscript.Echo objTapeDrive.Name     Next End If 

Scripting Steps

Listing 8.4 contains a script that retrieves the hardware inventory for a computer. In this script, information is retrieved for only a single hardware category: the pointing device. To retrieve information about additional hardware categories, the script would need to connect to each additional hardware class (for example, Win32_SoundDevice) and retrieve the appropriate property values.

To carry out this task, the script must perform the following steps:

  1. Create a variable to specify the computer name.
  2. Use a GetObject call to connect to the WMI namespace root\cimv2, and set the impersonation level to "impersonate."
  3. Use the ExecQuery method to query the Win32_PointingDevice class.

    This query returns a collection of all the pointing devices (mice, trackballs, and similar hardware) installed on the computer.

  4. For each pointing device in the collection, echo the values for the hardware type, number of buttons, pointing device status, and Plug and Play device ID.

Note

  • In a production script, you would probably save the inventory information from multiple computers to a database or text file rather than echoing it to the screen. For more information about saving data, see "Creating Enterprise Scripts" in this book.

Listing 8.4   Inventorying Computer Hardware

1 2 3 4 5 6 7 8 9 10 11 
strComputer = "." Set objWMIService = GetObject("winmgmts:" _     & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colMice = objWMIService.ExecQuery _     ("SELECT * FROM Win32_PointingDevice") For Each objMouse in colMice     Wscript.Echo "Hardware Type: " & objMouse.HardwareType     Wscript.Echo "Number of Buttons: " & objMouse.NumberOfButtons        Wscript.Echo "Status: " & objMouse.Status     Wscript.Echo "PNP Device ID: " & objMouse.PNPDeviceID Next

send us your feedback Send us your feedback « Previous | Next »   


Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 635

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net