Create Site-Specific Printer Mappings for Mobile Users

Make manual mappings a mere memory

Alex Angelopoulos

February 22, 2009

8 Min Read
ITPro Today logo in a gray background | ITPro Today



Providing local print services for mobile users can be a perpetual problem in small-to-midsized businesses (SMBs) with multiple sites. Currently, there is no out-of-box solution for providing location-based print services to mobile users for OSs earlier than Windows Vista. Starting in Vista, Microsoft offers an out-of-the-box solution for location-based printer assignment, but it isn't very useful for SMBs for two reasons. First, the assignment policies require an Active Directory (AD) structure that accurately reflects WAN topology. Second, the assignment policies take effect only after a Group Policy refresh.

It's possible to solve this kind of problem with a script; it just takes some scripting skills and some time, which is often in short supply in a hectic network. The central problem is figuring out where a user is at when they log on to a network. The most reliable technique for determining a user's location is to get the network ID for the local system, then match that address to a known location. Although you can summarize the solution very simply as "if a computer is on network X, map printer group Y," scripting that solution is far from simple.

First, you need to get the network ID for the local system. You can obtain the information needed to calculate this ID from Windows Management Instrumentation's (WMI's) Win32_NetworkAdapterConfiguration class. However, you need to get the values of two properties—IPAddress (which contains IP addresses) and IPSubnet (which contains subnet masks)—then perform a bitwise AND operation on those values to get the network ID. To further complicate matters, computers generally have several different network interfaces, so IPAddress and IPSubnet are arrays that typically contain multiple IP addresses and subnet masks, respectively. Even when there's only one IP address or one subnet mask, you still need to loop through the property to extract it because WMI will return that IP address or subnet mask as an array.


If you're lucky, you'll only have one network ID at this point. However, in all likelihood, you'll have several network IDs, with no direct way to determine which one is used for the LAN connection. To determine the correct one, you need to compare the network ID for the local computer to the network IDs for each site until you find a match.

Once a match is found, you aren't completely done. It's possible that the mobile user is running a Terminal Server session. You don't want to map printers for a Terminal Services session because printers located near the Terminal Server are by definition almost certain to be far away from the user. So, you need to account for this situation.

Finally, you need to handle two other possibilities. You need to handle situations in which there are multiple matches and no matches.

The AddressBasedPrinter.vbs script in Listing 1 does all this. When used with an existing network logon script, AddressBasedPrinter.vbs automatically determines which site a mobile user is in, then maps the appropriate printers at that site.

To implement this solution, you don't need to understand how AddressBasedPrinter.vbs works. (Although if you're interested, see the sidebar "How AddressBasedPrinter.vbs Works.") Instead, you just need to follow these steps:

  1. Pick the site names.

  2. Download and open AddressBasedPrinter.vbs.

  3. Customize the code in callout B.

  4. Customize the code in callout D.

  5. Test AddressBasedPrinter.vbs.

  6. Integrate with existing network logon scripts.

  7. Update AddressBasedPrinter.vbs as needed.

Step 1: Pick the Site Names

The first step is to pick a name for each site that contains printers mobile users might need to use. They should be simple, recognizable names. The names don't need to be elaborate because they're used only for matching purposes in the script. Write down each site's name along with its network ID for the LAN connection.

Step 2: Download and Open AddressBasedPrinter.vbs

The next step is to download AddressBasedPrinter.vbs. To do so, click the Download the Code Here button at the top of the page. Open AddressBasedPrinter.vbs in a text editor, such as Notepad. (To open it in Notepad, right-click the script and click Edit.)

Step 3: Customize the Code in Callout B

In AddressBasedPrinter.vbs, find the code in callout B. This is where you need to perform your first customization. Customizing this content doesn't require much scripting knowledge. All you need to do is specify each site name and its network ID following the format:

Locations("SiteName") = "NetworkID"

where SiteName and NetworkID are the site's name and network ID, respectively.

In AddressBasedPrinter.vbs, I defined five sites, but you can have fewer or more if needed. If you have fewer than five sites, delete the unused lines. If you have more than five sites, copy and paste one of the existing Locations lines for use as a template.



Step 4: Customize the Code in Callout D

In AddressBasedPrinter.vbs, find the code in callout D. You first need to customize the site names in the Case clauses of the VBScript Select Case statement. The site names must precisely match the names you used in the code in callout B. For example, if you specified "New York" for a site, the Case clause must read

Case "New York" 

If you use different capitalization (e.g., "new york") or different spacing (e.g., "New    York"), the script won't work correctly.

Once again, if you have fewer than five sites, delete the unused Case clauses in callout D. (Don't delete the Case Else clause.) If you have more than five sites, copy and paste an existing Case clause for use as a template. Next, you need to insert the following line under each Case clause:

WshNetwork.AddPrinterConnection "PrinterName", "PrinterPath" 

where PrinterName is the local display name you want to assign to the printer and PrinterPath is the share path to the printer. In callout D, you'll find an example of this line. In this example, the code is adding a mapping to the CentralPrinter printer shared on the NYPS01 server. This server will have the local display name of Default.

You can define as many printers as desired for each site. By default, these printer mappings won't be stored in the user's profile and will disappear when the session ends.

Finally, in the code in callout D, remove any lines that begin with an apostrophe (i.e., the sample mapping code and the lines that start with ' Code to map). The customizations are now complete, so you can save the script and close Notepad.

Step 5: Test AddressBasedPrinter.vbs

Testing AddressBasedPrinter.vbs before you integrate it with your network logon scripts is much easier than testing it after the integration. I recommend that you use the following process to make sure it works correctly for the sites you support.

First, at one of the sites, run AddressBasedPrinter.vbs from a laptop that has no existing local printer mappings to confirm that the script correctly maps printers without raising any errors. Then, share the script with a mobile user at each site and have that person run it the same way to confirm that the local printer mappings work. If they work, you're ready to integrate AddressBasedPrinter.vbs with your network logon scripts.

Step 6: Integrate with Existing Network Logon Scripts

AddressBasedPrinter.vbs is designed for use with existing network logon scripts. If you currently use VBScript logon scripts, you can insert the contents of AddressBasedPrinter.vbs into the logon scripts at each site. Alternatively, you can call AddressBasedPrinter.vbs as an external script from virtually any kind of network logon script, including .cmd and VBScript logon scripts.

To use AddressBasedPrinter.vbs as an external script, you need to place it in the same directory as your network logon scripts at each site. Assuming you've kept the name AddressBasedPrinter.vbs, you can call it from a .cmd logon script with the line

cscript %dp0 

To call AddressBasedPrinter.vbs from a VBScript logon script, you need to add the following code snippet to your logon script:

Set WshShell = CreateObject("WScript.Shell")dir = Left(WScript.ScriptFullName, _  Len(WScript.ScriptFullName) - Len(WScript.ScriptName)) PrintScript = dir & "AddressBasedPrinter.vbs"WshShell.Run PrintScript 

Step 7: Update AddressBasedPrinter.vbs as Needed

As part of your network maintenance activities, you'll need to update AddressBasedPrinter.vbs when necessary. For example, you'll need to update the script whenever you add a new site to your WAN or whenever you remove, add, or rename shared printers and print servers on local networks.

Give It a Trial Run

If you provide local printer support to mobile users who visit multiple company sites, automating printer assignment with AddressBasedPrinter.vbs is probably well worth a trial run. You'll likely reduce the amount of time you spend providing printer support to mobile users. Plus, mobile users' productivity will likely increase because they'll no longer have to wait for printer support.

Wondering about the code in callouts A, C, and E in Listing 1? The sidebar "How AddressBasedPrinter.vbs Works." will give you the scoop on this code.

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like