The Code Behind the Printer Mapping Utility
Part 2 explains this script in detail and tells you how to customize it for your environment.
April 9, 2002
How the PrintersMap.asp script works
Do you have a scripting-related question or problem? You can send your question or problem to [email protected].
In "A Printer Mapping Utility," April 2002, InstantDoc ID 24368, I introduced a utility that maps printer locations on a floor plan and lets users select a printer to install. I provided an overview of how the utility works and described the steps you need to take to prepare to implement it. In this second installment of a three-article series, I discuss the details of the PrintersMap.asp script. But first, I explain how to customize it for your network environment.
Customizing the Script
As I explained in "A Printer Mapping Utility," Default.htm presents a form from which a user can select a floor and printer. I explained how to modify the file's INPUT tag values and descriptive text to correspond to your floor maps and printer locations. Now, you need to modify the utility's VBScript file, PrintersMap.asp, to accommodate those changes.
In PrintersMap.asp, you must change the Case values and variables to match the information you modified in Default.htm and the floor map names you use. You'll find PrintersMap.asp in PrintersMap.zip in the Code Library on the Windows Scripting Solutions Web site (http://www.winscriptingsolutions.com). Download the file and locate the code that callout A in Listing 1, page 14, shows. Then, modify this code to correspond to the changes you made to Default.htm. For example, if you modified Default.htm so that the radio button that points to a print server named Server1 on the sixth floor points instead to a print server named Server10 on the ninth floor, you'd change the first Case value in PrintersMap.asp from
Case "Sixth" sPrnSvrName = "Server1" sFloorMap = "floor6.gif" sFloorName = "Six"toCase "Ninth" sPrnSvrName = "Server10" sFloorMap = "floor9.gif" sFloorName = "Nine"
If you added INPUT tags to Default.htm, you must add corresponding Case clauses to PrintersMap.asp.
How PrintersMap.asp Works
When a user selects a floor from the selection page and clicks Submit, Default.htm loads PrintersMap.asp, which displays a map showing the printer locations on the selected floor. Clicking a printer icon displays the Internet Printing Protocol (IPP) interface, which Figure 1 shows. From that interface, users can view additional printer attributes. Windows XP and Windows 2000 users can click the Connect link to install the printer to the desktop (the Connect link doesn't appear on earlier Windows clients).
Connecting with the CIMv2 Namespace
PrintersMap.asp is the heart of the printer mapping utility. This script obtains the floor value (e.g., sixth, seventh) from Default.htm. As callout A shows, the script uses this value to initialize three variables: sPrnSvrName, whose value is the name of the print server that serves the selected floor; sFloorMap, whose value is the name of the background floor map to display; and sFloorName, whose value is the floor name.
Armed with the print server name, PrintersMap.asp uses a Windows Management Instrumentation (WMI) connection string to attach to the CIMv2 namespace on the print server. Microsoft uses the CIMv2 namespace, which is defined by the Distributed Management Task Force (DMTF), to store systems management data. Other namespaces are available, but the CIMv2 namespace is where Microsoft stores the printer classes, which in turn define the attributes available to each printer. PrintersMap.asp displays some of the printer attributes on the floor map. As callout B, page 14, shows, the script connects to the CIMv2 namespace, then calls a subroutine to check for connection errors. When the connection is successful, the script continues.
Querying WMI Printer Classes
PrintersMap.asp uses the two WMI queries that callout C, page 14, shows to obtain printer attributes from Web-Based Enterprise Management (WBEM) classes that are part of the CIMv2 namespace. The first query uses the Win32_Printer class to collect data about all shared printers local to the print server. Local printers include network-attached printing devices as well as devices connected directly to local ports. To get PrintersMap.asp to display only locally attached printers, the script limits the query by checking the ServerName attribute of the Win32_Printer class. When the attribute value is NULL, the printing device is local. The script stores the query data in the PrinterSet variable, which is an SWbemObjectSet object. The script later uses PrinterSet to display printer attributes. The second query uses the Win32_PrinterConfiguration class to determine whether the printing device supports color printing. The script stores this query data in the PrinterConfigSet variable, which is also an SWbemObjectSet object.
After each query, a function checks for query errors. If no errors are found, the two queries were likely successful. In this case, PrintersMap.asp removes the connection object, oConnectCIMv2, from memory and calls the DisplayBackground subroutine to display the appropriate floor map image and title.
Checking Data Validity and Displaying Printers
For each SWbemObjectSet object in the two SWbemObjectSet collections, a For Each...Next statement evaluates the PrinterSet object and uses string manipulation to extract data. Another For Each loop evaluates the PrinterConfigSet object and selects an appropriate printer image based on the printer's configuration. After running an error-checking function, the PrinterSet loop checks that a value exists in the object's printer location field (Item.Location) and that a printer share name is assigned (Item.ShareName). The location field appears when you run the Add Printer Wizard to set up and share a printer; this field also appears on the General tab of a printer's Properties sheet.
If a location isn't specified or a share name isn't assigned, displaying the printer on the map is pointless, so the loop skips the record and evaluates the next record. When the location field contains data, string manipulation routines extract data and ensure that the end of the location field contains properly formatted pixel values. As I explained in "A Printer Mapping Utility," the end of the location field must contain pixel values in the form (pixels from the top of the floor map x pixels from the left of the floor map). For example, if a printer icon should appear 120 pixels from the top of the floor map and 300 pixels from the left, the values at the end of the location field must be (120 x 300). The parentheses are necessary for PrintersMap.asp to display the printer icon.
Invalid location field data causes error subroutines to display explanatory messages to the right of the floor map, as Figure 2 shows. Users can see this information, but it doesn't obstruct any part of the map. After PrintersMap.asp writes properly formatted pixel values to the printer's location field, a user simply refreshes the browser to view the printer icon on the map. Determining pixel dimensions can be tricky, but the more printers you configure, the more quickly you'll be able to determine appropriate placement. In the third article of this series, I provide an administrative interface that lets you easily determine printer placement on the map. After you select a location, a script in the administrative interface writes to the printer location fields.
PrintersMap.asp displays on the map local shared printers that include valid pixel dimensions. The script's SelectImage function uses the data in the PrinterConfigSet variable to select a monochrome icon for noncolor printers and a color icon for color printers. The script extracts the printer resolution from the PrinterSet object. Then, PrintersMap.asp determines the printer status. When the printer status is unknown, idle, or printing, the script gives the printer icon a green border; otherwise, the icon has a red border. The DisplayPrinter subroutine uses an HTML
tag to position the printer icon, assign a URL to the printer's IPP interface, show the printer image, and display printer attributes on the map. Before ending, the script removes the PrinterConfigSet and PrinterSet object values from memory.
Items to Check
Before you use PrintersMap.asp, make sure that each of your print servers is running Win2K or later. Also, verify that each print server is running IPP and WMI. To test whether IPP is operating properly, start your Web browser and go to http://print_server_name/printers, where print_server_name is the computer name of the print server that you want to check. To verify that WMI is running, make sure that the Windows Management Instrumentation service shows a status of Started in the Services console.
You and your users will likely find this printer mapping utility useful. An added benefit of PrintersMap.asp is that it gives you a sense of how powerful WMI is for systems management and administration.
About the Author
You May Also Like