Friday, October 26, 2012

Using a Script to Add a Network Printer with Custom Drivers





You can use a simple .vbs file to add a printer to a Windows 7 computer. This script will create a new TCP/IP local port for use with the printer.  Before you begin you will want to get ahold of the drivers that will allow this printer to be used on your target system (32 or 64bit).

Often the drivers are gathered in a folder of the download the manufacturer provides. You may need to sift through some of the files to find the correct .inf file for your printer. If you view the contents of the .inf file you may find a number of printers listed. It's important to note how your printer model is designated. You will need to specify the exact text of the printer listing in the /m switch of the printui.dll command.

One way to get the exact name of the printer you need to specifiy is to start the installation on a test system using the "Add Printer" wizard in "Devices and Printers".  Once you get to the "Install the printer driver" step click on "Have Disk..."  Browse to the .inf file and hit OK.  You will be presented with a list of printers. Using the exact text of one of options presented in the selection box as your printer listing text will achieve the desired result. You can cancel out of the wizard once you have the text copied to your script.



Let's assume the following scenerio:
- We have a networked Dell 5350 at network address 192.168.1.70
- The printer is in room 207 of your building
- You have pushed the drivers for the printer to a local folder (C:\Drivers\Dell 5350dn) on your target system.
- You want the printer name to show up as "Room 207 - Dell Laser" 
- The local port name will match the IP address.


Save the following into a text file and rename the file extention to .vbs

Set WSHNetwork = WScript.CreateObject("WScript.Network")
set shell = WScript.CreateObject( "WScript.Shell" )
CompName = shell.ExpandEnvironmentStrings("%COMPUTERNAME%")
Set objWMIService = GetObject("winmgmts:\\" & CompName & "\root\cimv2")

Set objNewPort = objWMIService.Get("Win32_TCPIPPrinterPort").SpawnInstance_
Set oShell = WScript.CreateObject("WScript.shell")
Set objPrinter = objWMIService.Get("Win32_Printer").SpawnInstance_

sub createPort (name, ip)
    objNewPort.Name = name
    objNewPort.Protocol = 1
    objNewPort.HostAddress = ip
    objNewPort.SNMPEnabled = False
    objNewPort.Put_
end sub


'-- Call the create port function with the address and port name parameters
 

createPort "192.168.1.70", "192.168.1.70"


oShell.run "cmd /K rundll32 printui.dll,PrintUIEntry /if /f ""C:\Drivers\Dell 5350dn\DKACLC40.inf"" /n ""Room 207 - Dell Laser"" /m ""Dell 5350dn Laser Printer"" /r
192.168.1.70 /b ""Room 207 - Dell Laser"" /q"

Set oShell = Nothing





Check your  "Devices and Printers" window to see if the new printer has appeared.

If you are finding that something is not working but you see that the port was created you can try just the following command on the target system.



rundll32 printui.dll,PrintUIEntry /if /f "C:\Drivers\Dell 5350dn\DKACLC40.inf" /n "Room 207 - Dell Laser" /m "Dell 5350dn Laser Printer" /r 192.168.1.70 /b "Room 207 - Dell Laser"

If things aren't working with just the command you may get an error in the form of 0x00000*.  This can often indicate that the driver file specified can't be found or is invalid.

3 comments:

  1. This is too good. So nice work. Can i use any laser printer for Plastic card printing purpose?

    ReplyDelete
    Replies
    1. If your plastic card printer is IP based and the driver can be installed using the printui.dll tool directly then you should be able to adapt this to your needs. If the printer is USB or parallel based you may need to call a different shell object.

      Delete
  2. This comment has been removed by the author.

    ReplyDelete

Let us know if you found anything helpful, or have an even better solution. Thanks for your participation in the discussion.