Rem: Mapping a Local IP Printer
Get a sample script that makes mapping a local printer a snap.
October 9, 2005
I'm a total newbie to VBScript, and I need to write a script that will automatically create a local IP printer for a new user. How should I go about doing this task?
The code you sent with your question looks like the "Install a Printer Driver not Found in Drivers.cab" code in the Microsoft TechNet script repository (http://www.microsoft.com/technet/scriptcenter/scripts/printing/ports/prpovb03.mspx). This repository is a good starting point when you need to know how to accomplish specific tasks from a script, but as you've no doubt discovered, not all the samples work as-is.
Often, the code in these samples must be modified to suit the task, and some sample scripts have errors, which is the case with the code that you sent.
Creating a TCP/IP-connected local printer consists of three steps: installing the printer driver, adding the TCP/IP printer port, and adding the printer. In Microsoft terminology, the printer is the software representation of the physical printer; the physical printer is called the printing device. Note that these steps create a local printer, not a network printer. Network printers are shared from a print server. Let's look at how to script these steps.
To create a TCP/IP printer, you can use a Windows Management Instrumentation (WMI)–based script such as CreatePrinter.vbs, which Listing 1 shows. Callout A in Listing 1 shows the part of the script that you'll need to edit to make it work in your environment. To determine the name to use for the DriverName variable, look inside the printer's .inf file. The DriverInf variable must contain the full path and filename for the printer's .inf file. The IPAddress variable should contain the printer's IP address.
CreatePrinter.vbs installs a third-party printer driver; to install a driver that comes with Windows, you'll need to modify the "Install Printer Drivers" sample code from the TechNet script repository (http://www.microsoft.com/technet/scriptcenter/scripts/printing/ports/prpovb04.mspx). To install a printer driver, the WMI connection must have the LoadDriver privilege enabled. (For more information about WMI privileges, see http://msdn.microsoft.com/library/en-us/wmisdk/wmi/privilege_constants.asp.) To add the driver, the script retrieves an instance of WMI's Win32_Printer class, sets the class's Name and InfName properties, then calls the class's AddPrinterDriver method. The Result variable will contain a zero value if the script successfully added the printer driver.
To add the TCP/IP port for the printer, the script creates an instance of the Win32_TCPIPPrinterPort class by calling WMI's SpawnInstance_ method. The script configures the HostAddress, Name, and Protocol properties of the class, then creates the port by calling the Put_ method. (Note that the trailing underscore characters are required for both the SpawnInstance_ and Put_ methods.)
The script then adds the printer by creating an instance of the Win32_Printer class. The script sets the class's DriverName, DeviceID, and PortName properties, then adds the printer by calling the class's Put_ method.
In creating this script, I used the minimum required properties needed for each step. (I determined the minimum required properties by trial and error.) For example, the AddPrinterDriver method of the Win32_PrinterDriver class will succeed when you specify at least the DriverName and DriverInf properties. Of course, you can specify other properties as needed in your environment.
Creating a printer this way is not without limitations. First, the WMI methods that CreatePrinter.vbs uses exist only on Windows XP and later. You'll have to use another method if you need to add printers to Windows 2000 clients. Second, all three steps require administrative privileges. Depending on your environment, this restriction might pose a problem. And third, creating local printers on separate machines can pose some logistical problems. For example, if the vendor releases an updated driver, you'll need to update that driver on multiple computers. Also, you'll have to manage printer permissions on each machine. Printers shared from a print server don't have these limitations. I've answered your question, but I recommend that you create TCP/IP printers only on a print server share that your client computers can then access.
About the Author
You May Also Like