Guy’s Scripting Ezine 118 – TCP/IP Printing

Guy’s Scripting Ezine 118 – TCP/IP Printing


This Week’s Secret

This week it is a pleasure to have Aidan as our guest script writer.  His TCP/IP printer VBScript helps me to appreciate the different ways that people write scripts and it helps you reader to see a variety of script writing styles.  Perhaps you make omelettes?  You may also go to restaurants and have an omelette.  When you go back home you probably add the omelette chef’s best ideas to your own creations, so it is with script writing.

Aidan is a professional script writer and he employs a different style from mine.  For example, Aidan declares his Dim statements just before he uses them, whereas I declare all mine in one section at the start.  My point is that it’s ok for you to write scripts your way, all that matters is that the code works!

Whether your are a professional, or a beginner do send in your scripts.  I can either add them to an existing page or feature them in a future ezine.

This Week’s Mission

Aidan’s script creates a local TCP/IP printer.  When you are new to TCP/IP printers it seems strange that they are local not network printers.  My rationale for understanding what is going on, is to think that a server acts as a surrogate mother for the JetDirect printer.  The crucial fact is that the printer has no operating system only a network card, that’s why it relies on a server to install the software and the printer port.

As with so many scripts, especially if you are entering a new area of expertise, a walk-through helps.  For this reason I suggest that you navigate to the Printers and Faxes folder.  From the menu, Select Add Printer, Local Printer, NOT Network printer.  Remove the tick from the Plug and Play box, move the radio button to Create a new port.  From the drop-down menu select Standard TCP/IP Port.  You should now see the diagram below.  If you have a real TCP/IP printer then you can finish the installation by adding the IP Address.

VBScript to create a TCP/IP printer


Example Script to add TCP/IP ports for your local printers


This is an advanced script which focuses on printui.  To make anything more than simple amendments you need to be experienced with VBScript.

There is no need for Active Directory as any Windows machine can act as the server.  However, to get the script working you need to:

Find and then edit:
IPArray = Array( "","","")
PrintersArray=Array("\\DC01\applications$\printers……. and following lines
PrinterNames=Array("EPSON AL-C4000 Advanced",_


  1. Copy and paste the script below into notepad or get a script editor such as OnScript.
  2. Save the file with .vbs extension e.g. TCPIP_Print.vbs
  3. Double click and observe the message box.



‘= By Aidan O’Donnell Ncc =
‘= Edit 22/06/2006 =
‘= This script will add ip ports for your local printers and attach printers to =
‘= those ports. Just edit the arrays to reflect the paths for the .INF files =
‘= and get the printer descriptions from the .INF files. If the printer is listed =
‘= in the system32/inf/ntprint.inf use that as the .INF it tells the script where =
‘= to look for the driver otherwise use the UNC path to get the path. =

strComputer = "."


‘= strComputer can be an array of netbios computer names or =
‘= ip addresses put into a for loop =

Public Error
Dim Count:Count=0
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Dim IPArray


‘= here is where your editing skills happen! =

IPArray = Array( "","","")
Dim PrintersArray

‘= here is where your editing skills happen again ! =


‘= here is where you edit the printer names =

Dim PrinterNames
PrinterNames=Array("EPSON AL-C4000 Advanced",_
"HP LaserJet 2200 Series PS (MS)","OKI whatever its called!")

For n = LBound(IPArray) To UBound(IPArray)
‘Install the IP printer ports
Install_IP_Ports IPArray(n)
For n = LBound(IPArray) To UBound(IPArray)
Count = Count + 1 ‘just increments the counter for the message at the end!

‘= Add the printers (Arguments: ip port, path to the .inf files, printer name) =
‘= get the name from the inf files or make one up! =

Add_local_ip_printer IPArray(n), PrintersArray(n),PrinterNames(n)
Make_Default_printer PrinterNames(1)

‘= if only one printer in array set to zero e.g. PrinterNames(0) or comment out!=

‘Install the TCP/IP Port(s):

Function Install_IP_Ports(strIPAddress)
Set objNewPort = objWMIService.Get("Win32_TCPIPPrinterPort").SpawnInstance_
objNewPort.Name = strIPAddress
objNewPort.Protocol = 2
objNewPort.HostAddress = strIPAddress
objNewPort.PortNumber = "515"
objNewPort.Queue = "LPT1_PASSTHRU"
objNewPort.SNMPCommunity = "public"
objNewPort.SNMPEnabled = False
‘stop the spooler
Set colServiceList = objWMIService.ExecQuery _
("Select * from Win32_Service where Name=’Spooler’")
For Each objService In colServiceList
errReturn = objService.StopService()
WScript.Sleep 1000

‘start the spooler

For Each objService In colServiceList
errReturn = objService.StartService()
WScript.Sleep 5000

End Function

Function Add_local_ip_printer(strIP,strPath_To_Inf_Files,strPrintername) ("CMD.EXE /C RUNDLL32 PRINTUI.DLL,PrintUIEntry /if /b "_
& chr(34) & strPrinterName & chr(34) &" /f " & chr(34) _
& strPath_To_Inf_Files & chr(34) & " /r " & chr(34) _
& strIP & chr(34) & " /m " & chr(34) & strPrinterName & chr(34) _
& " /q /u"),0,true
End Function

Function Make_Default_printer(strPrinterName)
Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * from Win32_Printer")
For Each objPrinter in colInstalledPrinters
If objPrinter.Name = strPrinterName Then
End If
End Function

Wscript.echo "script completed " & count & " Printers Added"
‘End Script

Learning Points – Printer

Note 1: Trace the WshShell object.  Understand how the variable executes the tricky PRINTUI.DLL and thus creates the printer ports.

Useful: Aidan kindly provided a copy of his comprehensive instructions and switches for printui

Note 2: I am not an expert in scripting arrays so I was interested to see the elegant: For n = LBound(IPArray) To UBound(IPArray). The ‘L’ in LBound means lower number, and the ‘U’ in UBound refers to the upper number.  A cruder technique would be to say For n=0 To 10, or For n=1 To 5.  The elegance of UBound and LBound is that the script calculates the values correctly, rather than you or I guessing the numbers.

Learning Points – General Tactics

Note 1: Admire how Aidan uses descriptive remarks.

Note 2: Observe how Aidan declares his variables just before he uses them.

Note 3: Be careful with the line endings.  I have added _ at the end of long lines, purely to make them fit on the page, you may wish to remove them and try the script.

Guy Recommends: Tools4ever’s UMRAUMRA The User Management Resource Administrator

Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.

It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information onUMRA.

Summary of TCP/IP Printing

When creating a script to install a TCP/IP printer you need to combine several pieces of knowledge, firstly you need to realize that the hardware has a network card with an IP address, but no operating system.  This means that another machine has to be the TCP/IP printer’s surrogate mother.  From a scripting point of view, focus on the very tricky pruintui command.

See more about logon printer VBScripts

Logon Printer Methods  • Ezines  • Logon Map Printer  • Logon Scripts  • Tool Kit

Ezine 2 Printers  • Ezine 5 Printers  • Ezine 16 Printers  • Ezine 17 Printers

Ezine 116 Printers Two  •Ezine 118 Printer network  •PowerShell Printers