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.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
.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
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
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