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

Computers and Internet Uncategorized

11 Comments Leave a comment

  1. 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?

Leave a 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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: