Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 


October 23, 2008

Avoid Ugly Computer Inventory Reports—Save Your Script's Output in Microsoft Word

VBScript script collects memory and process data, then writes it to a customizable, easy-to-read report
RSS
Subscribe to Windows IT Pro | See More VBScript Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Download the Code Here

Microsoft Word is the standard documentation tool used at my company, so when I recently had to write a script to retrieve memory and process information from computers, the information had to be written to a Word document. Using Word in a VBScript script isn't hard, and you can create some easy-to-read reports. For example, the script that I wrote, AutoInventory.vbs, creates a report that features a title page and nicely formatted data. As Figure 1 shows, the title page includes an image; both the report's title and image are customizable. As Figure 2 shows, the memory and process information is put into tables for easy reading. In Figure 2, notice the Your comment here header in the upper right corner. You can customize this header.

AutoInventory.vbs works on Windows Vista, Windows XP, and Windows 2000 Professional machines. Windows Script Host (WSH) 5.6 or later and Word 2003 (version 11.0) or later must be installed on the computer. You can download AutoInventory.vbs and a sample image file (logo.jpg) by clicking the Download the Code Here button at the top of the page. Place the script and image file in a folder on your local hard drive. Before you run the script, though, you need to customize the report's title, image, and header.

Customizing the Report

Changing the report's title, image, and header is easy. Simply follow these instructions:

Title setup. To customize the title, go to the title_property subroutine in Section 20. (The script is divided into sections—more on this later.) In the code

objSelection.Font.Name = "Arial"
objSelection.Font.Size = "55"
objSelection.Font.Emboss = True
objSelection.Font.Color = wdColorBlue
objSelection.ParagraphFormat.Alignment = CENTERED
objSelection.TypeText "Inventory Report" 

replace Inventory Report with your report title. If desired, you can change the title's font, size, embossing, and color by modifying the Font.Name, Font.Size, Font.Emboss, and Font.Color property values. For information about these properties, see the Font Object Members web page. You can also change the title's alignment by changing the ParagraphFormat.Alignment property value. See the ParagraphFormat Object Members web page for information about the Alignment property.

Image setup. To customize the image on the title page, place your image file in the directory in which you stored AutoInventory.vbs. Then, go to the ins_image subroutine in Section 20. In the code

Set myImage = objSelection.InlineShapes.AddPicture( _
  "ImageFilePathname",False,True)
  .Height = 400
  .Width = 450 

replace ImageFilePathname with the path and filename of your image file (e.g., C:\Scripts\logo.jpg). If desired, you can change the size of the image by modifying the Height and Width property values. For information about these properties, see the InlineShape Object Members web page.

If you don't want an image on the title page, you can cancel the call to the ins_image subroutine in Section 5. In the line

call title_property() 

add a single quote to the beginning of the line so that it looks like

' call title_property() 

Adding this quote causes the VBScript engine to treat that line as a comment rather than code.

Header setup. To customize the header, go to the headers_footers subroutine in Section 20. In the line

objDoc.Sections(1).Headers(1).Range.Text = "Your comment here" 

replace Your comment here with the header you'd like to use.

Running the Script

To run AutoInventory.vbs from the command line, open a command-shell window, go to the directory in which you copied the script and image file, and run the command

cscript autoinventory.vbs 

AutoInventory.vbs begins by checking the WSH and Word versions on your machine. If the required versions aren't present, the script quits. Otherwise, it displays the WSH and Word versions onscreen and starts to build the document, which you'll see in the background. The script first creates the title page, after which it retrieves and writes the computer's name and IP addresses to the report. If the computer has more than one network card, the report includes the IP address and MAC address (physical address assigned in univocal way to every network card) for each network card, as Figure 2 shows.

Next, the script creates the table that holds the memory information. To populate that table, it uses Windows Management Instrumentation's (WMI's) Win32_PhysicalMemory class to get information about the physical memory devices that are available to the OS.

After writing the memory data to the table, AutoInventory.vbs creates and populates the table holding the processes' names and status. The script uses WMI's Win32_Service class to retrieve the process data.

The script then saves the report, after which it displays a message box that specifies the filename assigned to the report and the directory where the report is located. (The script saves the report in the same directory in which you stored AutoInventory.vbs.)

How the Script Works

In case you're interested in seeing how the code in AutoInventory.vbs works, I've broken down the script into 20 sections:

  • Section 1 contains the constant declarations.
  • Section 2 checks and returns the WSH and Word versions.
  • Sections 3 through 5 create the report's title page.
  • Section 6 retrieves and writes the computer's name and IP addresses.
  • Sections 7 through 12 create and populate the table holding the memory data.
  • Sections 13 through 17 create and populate the table holding the process data.
  • Section 18 formats the tables.
  • Section 19 saves the report.
  • Section 20 contains the subroutines used in the script.

In each section, you'll find comments on how the code works.

Adapt as Needed

Besides customizing the report's title, image, and header, you can adapt AutoInventory.vbs to retrieve additional or different computer information. For example, you can collect BIOS information by using the Win32_BIOS class. (You can find information about the available WMI classes in Win32 Classes web page.) As you can see, AutoInventory.vbs is highly adaptable, so you can use it to produce a variety of easy-to-read reports.

—Mauro Magni, systems engineer

End of Article



Reader Comments
Brilliant!

ashers2008 October 28, 2008 (Article Rating: )


I'm getting an error and no report data. The error is (just after "I'm building the document":

c:\inventory\inventoryreport.vbs(297,1) Microsoft Word: This is not a valid file name.

Any ideas why it's trying to name the report inventoryreport.vbs?

Cheers,
Gary

boswortg December 19, 2008 (Article Rating: )


I emailed Mauro Magni about your experience with AutoInventory.vbs. Hopefully he'll know what the problem is and how to solve it.

Thanks for reading Windows IT Pro.

Karen Bemowski, senior editor
Windows IT Pro, SQL Server Magazine

KBemowski December 19, 2008 (Article Rating: )


You must log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
No Jobs, No Excitement at Apple's Last Macworld Keynote

Apple CEO Steve Jobs made the right move in skipping out on his company's last appearance at Macworld: In a Tuesday keynote address at the conference, Apple had no interesting new products to sell, opting instead to spend mind-numbing amounts of time on ...

CES 2009: Ballmer Announces Windows 7, Windows Live, Live Search Milestones

During his first-ever Consumer Electronics Show (CES) 2009 keynote address last night in Las Vegas, Microsoft CEO Steve Ballmer announced the pending public availability of a feature-complete Windows 7, the final version of Windows Live Essentials, and ...

Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...


Related Articles Script Inventories Remote Computers and Writes the Information to a Database

System Inventory Mini-App

Updating System Inventory Databases

Scripting with Word

Windows OSs Whitepapers Why SaaS is the Right Solution for Log Management

Related Events Virtualization Forum: Optimizing Storage, Networks, Desktops, and Security

Cloud Computing Forum: Integrating Software, Server and Storage as a Service into Your Enterprise IT Delivery Model

Virtualization Forum: Optimizing Storage, Networks, Desktops, and Security

Check out our list of Free Email Newsletters!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.


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 Windows Dev Pro IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2009 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing