Streamline migrations and configurations
Server upgrades, migrations, and increasingly consolidations comprise one of the most important classes of jobs a network administrator performs. The success of such projects relies on the administrator's ability to correctly and efficiently gather all the necessary information about an existing server's configuration so that a transition to a new server occurs with minimal disruption. If you're adept at keeping documentation, the job should be a breeze. But if you either keep poor documentation or have inherited servers that have undocumented configurations, you're in for some investigative work. To prepare for a server-migration project for one of my clients, I had to document the printers installed on the client's Windows 2000 Server print servers. We wanted to create a simple way to capture the printer information so that we could later recreate it on the new Windows Server 2003 server.
The Microsoft Windows 2000 Server Resource Kit's Printer Migrator utility (printmig.exe) is a great tool for easily migrating printers from one server to another. It has a simple GUI from which you can recreate printers, and it even automatically loads drivers for you.
The tool works great if you're migrating printers among servers that have the same installed OS or if the original server's print drivers are directly compatible with those of the new server. Unfortunately, in my case, the client had some print devices that required separate print drivers on Windows 2003 than on Win2K. In addition, Printer Migrator simply hung when we tried to back up the large numbers of printers on the server. We decided to go the manual route, which meant manually recreating each printer on the new Windows 2003 server. After accomplishing this task, we still had to find a way to get the necessary information into a spreadsheet so that we could create the printers without needing to go back and forth between the two servers.
Obtaining Printer Information
The information we needed to capture was the printer name, description, driver name, port name (which in this case, conveniently contained the TCP/IP port's IP address), share name, location, and comment. We could have retrieved this information by querying the HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Printers registry key. If we were to use the registry editor or the Reg.exe command to export this key, we would get more information than we wanted and the data certainly wouldn't be in an easily readable format.
An alternative solution would be to use a Windows shell script to parse this information, but the most elegant solution was to use Windows Management Instrumentation (WMI) to query the Win32_Printer provider and obtain all the printer information we needed, as well as write the data to a tab-delimited file that we could then import into Microsoft Excel. This solution would ultimately serve as a reference for the migration process and as a reusable script for documenting the configuration of print servers across the board.
Understanding the Script
The PrinterInfo.vbs script, which Listing 1 shows, starts by reading in the two required command-line parameters—namely, the computer name we want to report on and the name of the output file. If the user specifies insufficient command-line parameters, the script simply outputs the syntax information to the screen and quits immediately, as you can see at callout A.
The script then uses the WMI service to connect to the user-specified computer name and, if successful, queries the Win32_Printer provider about that PC. This statement returns a collection of all the installed printers, which we reference by the colPrinters variable, as you can see at callout B. Our script assumes that users will execute it under a security context that has permission to query the desired computer. This assumption is indicated by the impersonationLevel parameter, which is set to impersonate.
The script's next step is to use the Scripting.FileSystemObject object to create the output file. To do so, the script uses the OpenTextFile method and passes in the filename we read in from the command-line arguments. This step creates the output file if it doesn't already exist and overwrites it if it does. The script then writes header information to the file to label each column of data we're going to retrieve. To get the data from the printer collection, we run the collection through a For Each...Next statement. The For Each loop iterates through each printer in the colPrinters collection and reads in the values for each printer's name, description, portname, sharename, drivername, location, and comment properties. The script combines this information to create a line of text, which it then writes to the output file, as you can see at callout C. The only additional logic in this For Each loop is to check whether the printer is shared. This information resides in the printer's Shared property. If this value equals true, the script outputs the printer's share name; otherwise, the script merely outputs the string "Not Shared" as the share name. The script ends by closing the output file to ensure that everything is properly written. The user can then import this file to Excel and print or save it for future reference.
Making the Script Work
This script runs on Windows 2003, Windows XP, Win2K Server, and Win2K Workstation against print servers running XP or later. This limitation exists because Win2K doesn't support the network, shared, and comment properties in the Win32_Printer class. The script depends on the availability of WMI on the computer you want to report on; by default, WMI is installed and available in all three aforementioned OSs. You'll also need to run the script under an account that has sufficient privileges to query the Win32_Printer collection. You can execute this script using cscript.exe or wscript.exe as the script host. The syntax for the script is
printerinfo.vbs computername outputfile
For example, if you want to run this script to get a printer report on a printer server called WIN2K3PRT01 and output it to a file called printers.tsv, you would enter
printerinfo.vbs WIN2K3PRT01 printers.tsv
Good for the Arsenal
Even if you don't plan to migrate your print servers any time soon, having this script in your arsenal will let you document your printer configuration for future purposes. Depending on your needs, you might require more or less configuration information. To find a list of all the properties that the Win32_Printer WMI provider supports, check out the MSDN Win32_Printer page at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_printer.asp.