Automate IP printer port and driver install

I’ve just had a nice little scripting challenge to solve:

ICTST have all moved recently to nice offices off of the High Street.  We have a nice new printer here and no print server, so we needed to automate the installation of a local printer IP port and the driver for the printer.

After a bit of testing I came up with the following method.  I’ve used WMI to create a local IP port and then used PrintUI.dll to deliver the driver and setup the printer on the new IP port.  I could have used various WMI queries to do all of it, but I like the fact that PrintUI.dll was created for just this purpose and it does it very well indeed.

As I’ve scripted it below, the script needs to be in the same folder as the driver including the .inf file.  It has been tested on Windows XP SP3 only so far.

The script also outputs a message box if it finds a printer already on the IP address port specified.

I’ve generalised some of the details:

Set wshShell = CreateObject("WScript.Shell")
strCurrDir = Replace(WScript.ScriptFullName, WScript.ScriptName, "")
strIPAddress = ""
strComputer = "."
strPrinterName = "RICOH MFP Office 2nd Floor"  '## Can be any text you want, this shows print dialogs.
strDriverName = "RICOH Aficio MP C3300 PCL 6"  '## MUST be the exact text as in the printer driver .inf file.
strLocation = "New Office 2nd Floor"           '## Can be anything
strInfFile = strCurrDir & "OEMSETUP.INF"       '## the full path to the printer driver .inf file.  In my example the VBScript was in the same folder as the driver & inf file.

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer Where PortName = 'IP_" & strIPAddress & "' ")
For Each objPrinter in colPrinters
    MsgBox "Unable to install printer, printer already found on port 'IP_" & strIPAddress & "'." & VbCrlf & VbCrlf & "Found:  " & objPrinter.DeviceID, vbExclamation + vbSystemModal, "Printer Port already assigned"
    WSCript.Quit 114001

Set objNewPort = objWMIService.Get("Win32_TCPIPPrinterPort").SpawnInstance_
objNewPort.Name = "IP_" & strIPAddress
objNewPort.Protocol = 1
objNewPort.HostAddress = strIPAddress
objNewPort.PortNumber = "9100"
objNewPort.SNMPEnabled = False

wshShell.Run "rundll32 printui.dll,PrintUIEntry /if /b """ & strPrinterName & """ /f """ & strInfFile & """ /r ""IP_" & strIPAddress & """ /m """ & strDriverName & """", 1, True

Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer Where DeviceID = '" & strPrinterName & "' ")
For Each objPrinter in colPrinters
    objPrinter.Location = strLocation
WScript.Quit 0
Posted in Work | Tagged , , , , | Leave a comment

Comments are closed.