Install a local TCP/IP printer using a VBS script

To install a printer using a TCP/IP port, I run the following script, which works perfectly.

Just change the sections in RED to suit.

……………………………………………………………………………………………….

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
objWMIService.Security_.Privileges.AddAsString "SeLoadDriverPrivilege", True

Install "10.10.65.23"

‘ Start the install of the printer

sub Install(strIP)
    InstallPrinterPort strIP
end Sub

‘==================
strBasePrinter = "Lvl 22 North HP"
strPrinterName = "HP Color LaserJet CP3525 PCL 6"
strINFPath = "\\10.10.65.241\PrinterInstallScripts\Lvl22NorthHP\CP3525PrinterDriver\hpc3525c.inf"
strIPPort = "IP_10.10.65.23"
Set objShell = CreateObject("WScript.Shell")
strCommand = "cmd /c rundll32 printui.dll,PrintUIEntry /if /b """ & strBasePrinter & """ /f " & strINFPath & " /r """ & strIPPort & """ /m """ & strPrinterName & """ & /Z"
objShell.Run strCommand, 1, True
‘==================

Sub InstallPrinterPort(strIP)
    ‘ First check whether the port exists already
    Set colInstalledPorts =  objWMIService.ExecQuery _
        ("Select Name from Win32_TCPIPPrinterPort")
    For each objPort in colInstalledPorts
        If objPort.Name="IP_" & strIP then exit sub ‘ We have a result, so no need to add port
    Next
    ‘ Add new printer port
    Set objNewPort = objWMIService.Get _
        ("Win32_TCPIPPrinterPort").SpawnInstance_
    objNewPort.Name = "IP_" & strIP
    objNewPort.Protocol = 1
    objNewPort.HostAddress = strIP
    objNewPort.PortNumber = "9100"
    objNewPort.SNMPEnabled = False
    objNewPort.Put_
end Sub

11 Comments

  1. NIce, Just what I was looking for . Thanks Marc You ROCK!

  2. Nice Script, quick question? is it posible to add location and comment in the Printer Properties off this script?

  3. Very nice script, but I keep getting the following error: “The arguments are invalid.” I’m a batch script writer, not vbs. What would cause this?

    1. I also get that same error on XP SP3

      1. It is the comment apostrophes that it cannot recognise, that is what gives the error

  4. Its posible to write strINFPath = “c:\driver\autorun.inf ? Can it be autorun.inf ? I have error 0x800f0214

  5. […] Install a local TCP/IP printer using a VBS script | Marc Kean – Jul 14, 2009 · To install a printer using a TCP/IP port, I run the following script, which works perfectly. Just change the sections in RED to suit. …… […]

  6. […] Install a local TCP/IP printer using a VBS script | Marc Kean – Jul 14, 2009 · To install a printer using a TCP/IP port, I run the following script, which works perfectly. Just change the sections in RED to suit. …… […]

  7. […] Install a local TCP/IP printer using a VBS script | Marc Kean – Jul 14, 2009 · To install a printer using a TCP/IP port, I run the following script, which works perfectly. Just change the sections in RED to suit. …… […]

  8. […] Install a local TCP/IP printer using a VBS script | Marc Kean – Jul 14, 2009 · To install a printer using a TCP/IP port, I run the following script, which works perfectly. Just change the sections in RED to suit. …… […]

  9. […] Install a local TCP/IP printer using a VBS script | Marc Kean – Jul 14, 2009 · To install a printer using a TCP/IP port, I run the following script, which works perfectly. Just change the sections in RED to suit. …… […]

Leave a Reply to Ike Hoth Cancel reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s