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 = "111.1.222.333"
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
Next

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

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
    objPrinter.Put_
Next
WScript.Quit 0
Posted in Work | Tagged , , , , | 17 Comments

17 Responses to “Automate IP printer port and driver install”

  1. TommyMyo says:

    Thanks very much.
    I can save a lot of time to install IP printer on remote site.
    Cheers!!!

  2. Jamesy says:

    Many thanks. this worked a treat!

  3. ian says:

    quick question, this looks exactly what i am trying to do but i want the port to be a local print file not an ip address. Any thoughts on this? I looked in the Win_32 provider but didn’t see a localprinter port option.

    any help would be greatly appreciated

    • Darren Collins says:

      Hi Ian, I’ve not programmatically set up a print driver to print to a file. If you just want to capture the print file after it’s gone through a normal printer driver (e.g. an HP print driver) then you can attach to the built-in ‘FILE:’ port which will prompt for a file name.
      But the file will not be in a human-readable format unless you use a specific file printer driver like a pdf or xps print driver. In that case when you install a driver like that it will usually create it’s own local port name.

      Good luck,
      Darren.

  4. chris says:

    Hi this doesnt work can any body help i saved it as a .vbs file is that correct. it will not run

    Set wshShell = CreateObject(“WScript.Shell”)
    strCurrDir = Replace(WScript.ScriptFullName, WScript.ScriptName, “”)
    strIPAddress = “172.25.10.37”
    strComputer = “.”
    strPrinterName = “kyocera” ‘## Can be any text you want, this shows print dialogs.
    strDriverName = “Kyocera TASKalfa 5551ci KX” ‘## MUST be the exact text as in the printer driver .inf file.
    strLocation = “New Office 2nd Floor” ‘## Can be anything
    strInfFile = “F:\print\5551cx\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
    Next

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

    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
    objPrinter.Put_
    Next
    pause
    WScript.Quit 0

    • Darren Collins says:

      Hi Chris, what error are you getting and on which line?
      When you install the printer driver manually is the driver name definitely identical to “Kyocera TASKalfa 5551ci KX”?
      And are you definitely executing the script in an elevated command line? VBScripts will always execute in the non-elevated (current) context so if you are executing manually for testing, you’ll need to execute cmd.exe with elevated privileges and call the VBScript from the cmd.exe window.

      Regards, Darren.

  5. Murat Terzioğlu says:

    Hi Darren,

    Is it possible to make same automation with javascript?

    Thanks

    • Darren Collins says:

      Hi Murat,

      Probably not – unless you are embedding the JavaScript into an HTA which, when ran elevated, will have the rights to the machine, but I really don’t know I’m afraid. But if you’re going to use an HTA then I suggest use VBScript.
      You certainly can’t put JavaScript into a web page and perform the install of a local printer because the security model of a browser will not allow local admin machine access even if the browser is executed elevated.
      You can use JScript in a .js file to do this though, which is very similar to JavaScript but is executed by the Windows Scripting Host.

      Hope this helps,
      Darren.

  6. Phillip says:

    Hi Darren, I am trying to set up a script which configres only the host names for me. For example printer1, printer2 and so on. But I am not able to figure a way out which is working. Do you have any idea or hint for me? Thank you very much in advance.

    • Darren Collins says:

      Hi Philip,

      I’m not sure what you mean by configures ‘only the host names’. Can you explain what you need to end up with?

      Darren.

  7. Phillip says:

    Hi Darren,
    I am locking for a script, which adds the windows ports for 50 Windows port names automatically. The manual way is:
    Devices and printers – printer properties – Ports – add Port – Standard TCP/IP Port – new Port – next – Printer Name -> myprinter001
    Any hint is appreciated.

    • Darren Collins says:

      Hi Philip, I will assume that the name of the port is to be the same as the hostname of the printer. The following script may work. Unfortunately I am away from the office so I do not have access to a VM to test this at all. PLEASE PLEASE test on a test computer and *not* on the live server / computer.

      1. create a file called NameList.txt with all your printer names in, one on each line, and place somewhere. In the first line of the script below it looks for ‘C:\temp\NameList.txt’ but you can change this.
      2. copy all lines from this pastebin:
      http://pastebin.com/Au0U35iw
      … and paste into a new test file called createports.vbs
      3. execute createports.vbs from an elevated command line.

      Let me know of any errors and I’ll see what I can do.
      Good luck!

      • Darren Collins says:

        … actually this script may work running not elevated as it is only creating ports and not installing the printer!

  8. Phillip says:

    Hi Darren,

    I have saved the file createports.vbs in ANSI file format, but I am getting the following error code 800A0408 in line 2 at character 24.

    • Darren Collins says:

      Hi Philip, sorry about that, it is because this blog changes the quotes from standard ASCII to smart quotes.
      I have placed an accurate copy on pastebin and tested it this time!

      1. create a file called NameList.txt with all your printer names in, one on each line, and place somewhere. In the first line of the script below it looks for ‘C:\temp\NameList.txt’ but you can change this.
      2. copy all lines from this pastebin:
      http://pastebin.com/Au0U35iw
      … and paste into a new test file called createports.vbs
      3. execute createports.vbs from an elevated command line.

      As before, let me know of any errors and I’ll see what I can do.
      Good luck!

      Darren.

  9. Phillip says:

    Hi Darren,

    Thank you very much for your help and the fact that you put your effort and knowledge into the little script. I was running the script and it was working exactly as promissed. I have created a text file with five host names and they have been created sucessfully.

    Best regards

    Phillip

    • Darren Collins says:

      Excellent, thanks for the feedback. You actually made my job easy by describing exactly how you would do it manually – this meant I could see what was created and helped me to automate what you required.

      Happy to help.

      Regards,
      Darren.