Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


Return to article

Home-Brewed Hardware Inventory
 

Your boss calls you and says, "Accounting needs a hardware inventory report by the end of the week. Oh, and don't even think about spending money on an inventory software package." What started out as a pleasant day has taken a turn for the worse.

You can manually inventory every computer in your company, you can quit your job, you can claim that the task can't be done—or you can use a two-pronged scripting solution that collects hardware inventory and generates a Microsoft Excel report in record time. This solution—which requires only Windows Management Instrumentation (WMI), Windows Script Host (WSH) 5.6, and Excel 2000 or later—can gather extensive inventory data from Windows Server 2003, Windows XP, Windows 2000, Windows NT 4.0 (Service Pack 4—SP4—or later), Windows Me, and Windows 98 systems.

Preparing for the Inventory
The hardware inventory solution consists of two scripts. The first script, HrdWrInv.vbs, is an inventory agent that collects hardware information from each computer on the network. The second script, BuildReport.vbs, is a report agent that creates an Excel workbook containing hardware inventory data from all the computers on which HrdWrInv.vbs ran successfully. You can download both scripts from http://www.winscriptingsolutions.com, InstantDoc ID 39111.

When the inventory agent runs on a system, the script creates a small text file, typically no larger than 6KB, and names the file HrdWrInv_ComputerName.txt (where ComputerName is the name of the inventoried computer). HrdWrInv.vbs places each file in a directory—which I refer to as the hardware-collection directory—that you specify for the strInvFilePath variable, which appears on line 22 of the script. Make sure that you specify a network path that all users who run the inventory agent can access, and follow the path with a backslash (\). Also, give users Read and Write access to the hardware-collection directory. The default path that I use in HrdWrInv.vbs is \\sea-fs-02\hw\, which points to the hw share on a file server named sea-fs-02; you'll need to replace this path with one suitable to your environment.

You can use a drive letter to specify the path (e.g., G:\inventory\hw), but if you do so, each user needs to map the drive letter to the same network location so that the script will store all the inventory files in the same place. If you specify a Universal Naming Convention (UNC) path, you won't need to worry about whether all users have mapped the drive correctly.

The inventory agent relies on WMI and WSH 5.6. Windows 2003, XP, Win2K, and Windows Me systems install WMI by default; ensure that the WMI Core 1.5 components (which you can download at http://www.microsoft.com/downloads/details.aspx?familyid=afe41f46-e213-4cbf-9c5b-fbf236e0e875) are installed on any NT 4.0 SP4 or later or Win98 clients that will run the script. Windows 2003 and XP systems install WSH 5.6 by default; for Win2K, NT 4.0, Windows Me, and Win98 clients, you can download WSH 5.6 at http://msdn.microsoft.com/library/default.asp?url=/downloads/list/webdev.asp.

After you prepare the script and machines to run the inventory agent, you need to make the script available over the network. The script is silent and can be called as part of an existing logon-script processing routine. If you support only Win2K or later systems, you can call the agent as part of a Group Policy logon or logoff script-processing procedure. Alternatively, you could modify the inventory agent so that it remotely connects to WMI clients and collects inventory from a central location. To learn about techniques for making remote WMI connections, see "Remote Administration with WMI," February 2003, http://www.winscriptingsolutions.com, InstantDoc ID 37596.

You'll know the agent is doing its job when text files starting with the prefix HrdWrInv_ begin appearing in the hardware-collection directory. (For details about the agent, see the sidebar "How the Inventory Agent Works.") The next time the inventory agent runs on a computer, the agent will detect that it has already taken an inventory and the script will exit. If you want to inventory a computer again, simply delete that system's inventory file from the hardware-collection directory. The next time the agent runs, it will create a new inventory file.



Analyzing the Data
After inventory collection is complete, you can review individual inventory files in Notepad. Each text file contains sections, such as ComputerSystem and IDEController, that represent a characteristic of the inventoried computer. Not every inventory file contains every section, however. For example, if you inventory a computer that contains a SCSI controller but no IDE controller, the corresponding file will contain a SCSIController section but won't contain an IDEController section.

Also, a system's OS version will affect the amount of hardware that the inventory file displays. For example, a Win98 system's inventory file won't contain a PhysicalMedia section because the Win32_PhysicalMedia WMI class is available only on Windows 2003 or XP machines.

Within the available sections, you might notice some items with values listed as Not available. This value occurs when a hardware manufacturer hasn't stored data for a particular WMI property or when WMI can't locate a property's value.

The inventory analysis demonstrates that a computer's OS and hardware components, as well as the properties of each hardware device, create exceptions to uniform inventory collection across all computers. Still, the script will leave you with a rich collection of useful inventory information.

Building the Report
Telling members of the Accounting team that they can go to your hardware-collection directory to view 5000 tiny text files containing the information they want probably won't sit too well with your boss. The next step, then, is to create a hardware inventory report. Enter our trusty report agent, BuildReport.vbs. The report agent reads each file, locates sections of inventory data, then writes the data to individual worksheets in an Excel workbook named InvReport.xls. (For details about how the script works, see the sidebar "How the Report Agent Works," page 4.)

Excel is a good choice for consolidating the inventory data for several reasons. First, Excel is widely available. Second, the application lets you easily create rich reports and analyze collected data. And third, most people who work with inventory reports are familiar with Excel spreadsheet operation and analysis.

Before running the report agent, you must make two minor changes to lines 9 and 10 of BuildReport.vbs. Line 9 contains the strInvFilePath variable and specifies the path to the hardware-collection directory; Line 10 contains the strReportPath variable and specifies the location in which you want the script to create InvReport.xls. Change these values to suit your environment.

Before you run BuildReport.vbs, you need to know three details about the report agent. First, the agent relies on the Excel application object. Therefore, make sure that Excel is installed on the computer on which you'll run the agent.

Second, I intentionally wrote the agent to display a processing message as it parses each inventory file; this regular output assures you that the script is successfully processing the inventory files. (When I ran the agent on a 1GHz Pentium III computer with 512MB of memory, each file required approximately 3 seconds to process.) Because of this output, you must use WSH's CScript host engine to run the script so that the script will continue after it processes each record. If you were to use WSH's WScript host engine, you'd need to click OK each time—a lot of clicking if you have 10,000 inventory files to process. To run the report agent with CScript, either make CScript your default host or open a command prompt, go to the directory that contains the report agent, and type

cscript buildreport.vbs

Third, consider the time the report agent will take to run before you begin the process. If each file takes 3 seconds to process and you have 10,000 files, complete report generation will require 30,000 seconds, or 8.3 hours. Therefore, consider running the report at night and on a fast computer that isn't busy completing other tasks.

Figure 1 shows an example of the output you'll see when the report agent finishes its work. (To show as much data as possible in this figure, I've hidden toolbars and reduced cell width.) The figure shows the Computer Systems worksheet; the workbook includes 14 worksheets in all, one for each section of inventory data: Computer Systems, Page Files, RAM, SCSI Controllers, IDE Controllers, Disk SerNums, Removable Media, Fixed Disks, Processors, NICs, Monitors, Video Adapters, Motherboards, and BIOS. You can modify the inventory agent and report agent scripts to collect more data from each computer if you want.

Inventory in a Nutshell
You now have a lightweight yet verbose inventory-collection and -reporting tool—and for nothing more than a subscription to this newsletter. I hope you, your boss, and the accounting department will all be thrilled.







Reader Comments

Great article but since the most important part in an inventory is the serial number, is there a way to get the serial number of the PC?--Don

If you want the serial number look at the IdentifyingNumber property of Win32_ComputerSystemProduct. Works on most computers built since 2001. Older computers may or may not report their serial number.--Bill Schalck, April 19, 2004

Don -August 07, 2003

Great code and just when I needed it!

Martin -August 14, 2003

When I run the inventory script against my PC, I get this error, which occurs after OSInformation, ComputerSystem, PageFileSetting, and PhysicalMemoryArray have been recorded in the .txt file:


---------------------------
Windows Script Host
---------------------------
Script: C:\Documents and
Settings\JeffV\Desktop\Hardware
Inventory\HrdWrInv.vbs
Line: 154
Char: 3
Error: 0x80041001
Code: 80041001
Source: (null)

---------------------------
OK
---------------------------

The line that errors out is this one in the QueryInstances subroutine:

For Each objComponent in objClassName

Jeff -August 17, 2003

Hi! Just wanted to make a small remark concerning the code:

First HrdWrInv.vbs, line 93 reads:

If Mid(intVersion,1,3) >= 5.1 Then

This should read:

If Mid(intVersion,1,3) >= "5.1" Then

or the script would not run on my WinXPPro.

Second, in BuildReport.vbs, line 295 reads:

RenameSheets "Sheet" & i,WBName

My Norwegian Office calls these sheets "Ark", and this script didn't run either, until I corrected this.

Thomas Hansen -September 11, 2003

This was very helpful in a bind. Code works well. Easy to read and modify!

Scott -October 22, 2003

I really enjoyed this article about the hardware inventory. It will probably get lots of use here. I have been looking for this exact code. We've been using a third-party software package for this, but since we've upgraded our security, that package no longer works.

I do have a few questions, though: What code should I put in if I want to collect the username on the Computer Systems tab in the Excel report? What needs to go in the hardware script AND what should I put in the report generator?

A note from Ethan: The Computer Systems tab is populated with information from the Win32_ComputerSystem class. In this class, there are two additional properties that will probably provide you with what you're looking for: UserName and PrimaryOwnerName.

In hrdwrInv.vbs, you need to add these two properties to the strProperties variable for Win32_ComputerSystem. You'll see the properties listed on lines 64 - 66. In buildReport.vbs, you need to add column heads for the two new properties. The column heads are specified for Win32_ComputerSystem on lines 49 and 50.

Carl Klima -January 22, 2004

This is going to work well for us. I modified a bit to get the UserName and SerialNumber. Now just to make it automatically run on all computers on the domain......

Craig -March 25, 2004

I was trying to get the SerialNumber property from the bios and entered the property in Line 133 in HrdWrInv.vbs and added the property on line 97 of the buildReport.vbs. The info is collected in the text file but doesn't show up in the Excel report. Did i omit something?

Jean-Claude -May 06, 2004

This is great. I have been reading up on the WMI classes and was trying to pull the network settings (ipaddress/DNS/GW, etc) but it always gives me errors. Any thoughts? I would also like to grab some software info. Is there an easy class that has the SW listed in ADD/remove only? What about hotfix/updates? Thanks for the great script!

Bill Prochazka -May 18, 2004

On Windows 2000 Pro i have got thi error: --------------------------- Windows Script Host --------------------------- Script: C:\Documents and Settings\Tommaso\Desktop\HrdWrInv.vbs Line: 154 Char: 3 Error: 0x80041001 Code: 80041001 Source: (null) --------------------------- OK --------------------------- I have already try to update to version 5.6 of scipt engine but i have got it again. What i can do to solve the problem

Tommaso -July 05, 2004

Great article/script. Straightforward and works great - unlike most resources out there! Ran into the same exact scenario with my accounting department and got all 130 servers and workstations inventoried in 2 days through Group Policy. Thanks for the help.

Rob Wurster -July 09, 2004

This is most useful. I was able to run the inventory script without error but when I ran the report script the processing inventory box appears but as soon as I press OK I get a script error. Line: 36, Char: 5, Error: File not found, Code: 800a0035. Line 36 only contains comments???

nbk24o8 -July 26, 2004

Works great, BUT anyone know how to detect the Serial Numbers of disk drives in a RAID? This program detects, "MEGA RAID 5" not enough info!

racer999 -August 05, 2004

Hi all, When i try to execute the script to colect data about windows 98 machine, this error apears: The remote server does not exists.... Cod: 800A01CE PS: Windows2k is ok.

Anonymous User -October 20, 2004

Not to sound LAZY, but its been a while since I've done any programming and thought I'd ask..even though I am being lazy. Can someone email me the script with the changes that will collect the username and password? joe.oldendick@gmail.com Thanks!

JoeOldendick -October 26, 2004

I mean the username..not the password...doh!! Now that is REALLY lazy not to proof read your post!

JoeOldendick -October 26, 2004

Hi all! Can somone please tell me how to add the string for computer serial number? I haved tried but the script does not create the .txt file like it did before. please help. Thanks. Tom thomasvarmfors@lfv.se

Anonymous User -December 09, 2004

great script!! is there a way to modify the scripts so that it can include software inventory as well. I want to be able to poll each machine and extract what software they have installed and the registrations keys and licenses and have it tied into the excel report. If possible can you send that added code. Thanks Nick

renz416 -June 22, 2005

When I run the buildreport.vbs, I am getting the following error: "(36, 5) Microsoft VBScript runtime error: File not found." The line it is complaining about is: "Set objTextFile = objFSO.OpenTextFile(strInvFilePath & strFileName,ForReading)" Anyone know whats up?

Anonymous User -August 22, 2005

can any one tell me what changes need to be made to get user info from the script. Thanks

Anonymous User -September 02, 2005

When I run this script on a remote computer(WinXP, SP2) , any one have any answers help much appreciated. Script: Script location Line: 27 Char: 2 Error: 0x80041001 Code: 80041001 Source (null) Line 27 is below which is causing the error. For Each objItem In objProduct

mnowell044 -October 12, 2005

Hey everybody, I receive the following error message when running script on W2K. --------------------------- Windows Script Host --------------------------- Script: C:\Documents and Settings\****\Desktop\HrdWrInv.vbs Line: 154 Char: 3 Error: 0x80041001 Code: 80041001 Source: (null) --------------------------- OK --------------------------- Does anybody have a solution for that problem??

Pharrell -March 15, 2006

sdfsdfsdfsdfsdfsdf

nateb4nateb4 -June 16, 2006

Very good

sanishgeorge -January 14, 2007

Where do i actually d/l the scripts from?? It says "You can download both scripts from http://www.winscriptingsolutions.com, InstantDoc ID 39111. . . . " Going there only leads to the article. I cannot find a location on the website to actually download the two scripts... can someone point me to the URL for both of them please...

drumgod007 -July 31, 2007

drumgod007: To download the code, click the blue Download the Code Here button at the top of the article (just under the title information and links to the sidebars).

AnneG_editor -August 07, 2007

Great script Ethan. Does anyone check these comments anymore? I have a quick question. I've added two extra wmi categories to the inventory script. win32_operatingsystem and win32_computersystemproduct. I've been trying to figure out how to add another worksheet for each of those categories to the spreadsheet report. But I keep getting an Input Past End of File error, no matter what I do. Can someone please explain to me how I have to modify the BuildReport script to get it to add the two workstheers?

jeremyschubert -August 20, 2007

This looks very useful but I suspect the code is no longer available due to age of article. If someone could help out id appreciate it.

djwoodward -February 25, 2008

Dj, the code is still available to subscribers. Just click the blue Download the Code button at the top of the article to download the zip file w/ the code.

AnneG_editor -February 25, 2008
Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement