We help IT Professionals succeed at work.

How to create a TCP/IP printer port via command line

MisterBedo
MisterBedo asked
on
I am trying to change a local printers port via a batch file but I'm running into problems.

The printer is currently set as LPT1 and I need it to be IP_192.1.1.25. If I use the command:

rundll32 printui.dll,PrintUIEntry /Xs /n "Canon 6800" Portname "LPT2:"

it changes the port to LPT2, so I know that this method does work.

But, if I use the exact same command but change the port to the IP address I want to use like this:

rundll32 printui.dll,PrintUIEntry /Xs /n "Canon 6800" Portname "IP_192.1.1.25"

I get an error telling me that the specified port is unknown.

I'm guessing that I first need to create the IP port and then use the above command to tell the printer to use it.

But how do I create the port via the command line ?

thanks in advance for any help !
Comment
Watch Question

Commented:
How does the printer connect to your computer?
Commented:
you need to great a regfile to create the TCP port

Setup the Registry File
The first step in creating the script is to setup a registry file so that the printer ports can be created if they're not already present on the system. You MUST be sure to do this. The second part of the script will NOT work if the appropriate ports are not installed! Decide on how you want to print to the printer. If you're printing directly to the printer via TCP/IP, your registry file will appear in the following format:

REGEDIT4

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports\<hostname>]
"Protocol"=dword:00000001
"Version"=dword:00000001
"HostName"="<hostname>"
"IPAddress"=""
"HWAddress"=""
"PortNumber"=dword:0000238c
"SNMP Community"="public"
"SNMP Enabled"=dword:00000001
"SNMP Index"=dword:00000001

Replace <hostname> with the fully-qualified domain name of the printer such as printername.domain.com. You may also have to change the SNMP parameters depending on if the printer has SNMP support enabled. The settings provided above are the typical default settings for all HP network printers.

If you want to print via an LPR port, your registry file will appear in the following format:

REGEDIT4

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Monitors\LPR Port\Ports\<hostname:printer>]
"Server Name"="<hostname>"
"Printer Name"="<printer>"
"OldSunCompatibility"=dword:00000000
"HpUxCompatibility"=dword:00000000
"EnableBannerPage"=dword:00000000

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Monitors\LPR Port\Ports\<hostname:printer>\Timeouts]
"CommandTimeout"=dword:00000078
"DataTimeout"=dword:0000012c

Replace <hostname> with the fully-qualified domain name of the printer server such as server.domain.com and <printer> with the name of the printer spool on the server such as printername.


NOTE:
After making changes to the registry, the Spooler service must be restarted for the new ports to be available.

Setup the RUNDLL32 Command
The next step is to setup the RUNDLL32 command to do the actual printer installation. The general format of the command is as follows:

rundll32 printui.dll,PrintUIEntry /if /b "<description>" /f "%windir%\inf\ntprint.inf" /q /r "<port>" /m "<driver>" /z /u

Author

Commented:
HM2K - It connects over the netowrk. It's a Canon multifucntion unit with it's own internal print server. Normally we would add a standard TCP/IP port with the 192.1.1.25 address using the add printer wizard, but I need to automate this as much as possible as we are about to add 50 new users to it.

pbohara - Thanks for that. I'm having problems using the vb script though. Whenever I use it to add a port I get the error message:

Unable to get port Error 0x80041002 Not found

I'll keep playing with it tho.

Commented:
you can make the reg file and call it from a script to automate the port creation

Commented:
I think that error means that either it can't find security settings (FAT partition?) or they're wrong.  Are the PCs on the same domain and/or are they both XP pro?

Commented:
just enter the ip in the "ipaddress=" field instead of the hostanme
you can creat the reg file with notepad, or simply creat the port and export the mentiond key and modify it to fit your needs .

Author

Commented:
Thanks all. I've sorted it now.

atkjedi - the reg edit does exactly what I need it to do. I did think about editing the registry but wasn't sure exactly which bits to add because I found a few references to that port in the registry after manually installing the port (if that makes any sense!!).

thanks again to those that replied.

Commented:
I just created a port on the PC of the guy sitting next to me using a slightly modified version of this command (to protect the innocent).

:)

cscript C:\windows\system32\prnport.vbs -a -s (remotecomputername) -r IP_1.2.3.4 -h 1.2.3.4 -u domain\username -w password -o raw -n 9100

Commented:
BAH!

well, whatever works.  good luck! :)

Commented:
Thats good to hear, I fought with this one for a while in the past

Good Luck!!!

Commented:
prnport.vbs works a lot better than the registry import. If you have a list of IP addresses in a txt file you wish to setup ports for, you can use VBScript to parse this file and create all of the ports automatically

You can also do this from command prompt:

for /f "tokens=1" %m in (path\PortList.txt) do cscript.exe C:\Windows\SysWOW64\Printing_Admin_Scripts\en-US\prnport.vbs -a -s (remotecomputername) -r IP_%m -h %m -u domain\username -w password -o raw -n 9100

(If using this in a batch file, use %%m instead of %m)