Many scripts redirect their text or HTML output to a text file, and some scripts even use automation to redirect their output to Microsoft Excel. These approaches are fine if you're reading the output on screen or emailing it to someone, but if you want to print the results, you need to open the file, then choose the Print option. If you typically want to print a particular script's results, you might prefer to have the script print the file. Let's look at the several ways you can print from within your scripts.
Simple Does It
The simplest way to print from a script is to use Notepad's print functionality. When you use the /p switch, Notepad prints the file to your default printer. For example, the following code causes Notepad to print C:\mylog.txt to your default printer:
Notepad /p c:\mylog.txt
Listing 1, page 2, shows a snippet of VBScript code that uses this command. This code should work fine as long as your PATH environmental variable includes the directory that contains Notepad; otherwise, you need to specify Notepad's full path.
The drawback to this approach is that it opens an instance of Notepad that remains on the screen. Consequently, you don't want to use this solution in a script that runs unattended. For such jobs, you can use one of the other options I describe.
Using CopyFile and UNCs
For unattended jobs, you can have the script send a file that it has created to a port on your computer that maps to the printer you want to use. Listing 2, page 2, shows how to use the FileSystemObject object's CopyFile method to copy the file to a printer represented by a Universal Naming Convention (UNC) path. You'll need to edit the strFilePath and strPrinterUNC variables in the code to specify the file to print and the printer to use on your network, respectively. Make sure that the file to be printed is formatted; the printer logic will truncate any lines that are too long to print on the page.
To find the correct name to use for a printer on your network, you can use the Scriptomatic tool (scriptomatic.exe, available at http://www.microsoft.com/technet/scriptcenter/tools/wmimatic.asp). From the tool's primary screen, select the Win32_Printer Windows Management Instrumentation (WMI) class. (For more information about using Scriptomatic, see "AD and WMI Reporting," May 2003, http://www.winscriptingsolutions.com, InstantDoc ID 38401.) If you want to devise a more automated solution, you can also use Scriptomatic to obtain the correct Win32_Printer properties to use.
What if you simply want to print to the default printer without looking up the printer's name? On later OSs that have expanded WMI classes, such as Windows Server 2003 or Windows XP, you can use the sample code that Listing 3 shows, which retrieves the port name of the default printer for you. To develop this code, I used Scriptomatic to show me which properties to manipulate. The code at callout A in Listing 3 connects to the root\cimv2 namespace on the local computer and retrieves a collection of the installed printers and their attributes. A For Each...Next statement then enumerates the collection, determines which printer is the default, and sets the strPrinterUNC variable to the PortName property. After exiting the loop, the script prints the file to the default printer's port. Because the WMI objItem.PortName property isn't available in Windows 2000, the code in Listing 3 doesn't work with Win2K. If you're running Win2K, you can use Scriptomatic on the Win32_Printer class to see the available printers and choose the one you want, then hard-code that printer's name in your script.
An alternative is to use the TextStream object's Write and WriteLine methods to write the text to a file, then use code like that in Listing 3 to print the file. Or, you can simply print each line to the destination printer port, then let the spooler print the file after all lines have been printed to the port. Listing 4 (another Windows 2003 and XP script) shows code that uses a simple For...Next statement to print the numbers from 1 to 50. The FileSystemObject object's OpenTextFile method provides a TextStream connection to the printer that will stay open until the TextStream object's Close method closes the connection and prints the document.