July 2006 Wayback

Script to find replace IP’s on local TCP/IP printer ports.

Our enterprise is going thru a domain wide IP migration and we needed a way to script out IP changes for printers mapped to local TCP/IP ports.

I found this post: Script to find replace Ip’s on local printer ports? and found a nice script from Kheldaroz. Which while it didn’t work correctly it was enough to get me going in the right direction. Take a look at the old script and then my changes here 1st, then the correct script below:

First create a ‘printerip.csv’ file with newIP in 1st column and oldIP in 2nd column. For some reason Kheldaroz has the Printer Name listed first. (?!)

Set WshShell = WScript.CreateObject(”WScript.Shell”)
If this line is not present you may receive and error if the script tries to make any changes.

During the following section of code, the script would point the printer to the new TCP/IP port, but on the 3rd line below it would change the old TCP/IP port to point at the new IP address, stranding the printer to an IP port that had not been created.

WSHShell.RegWrite print1 & “Port”, “IP_” & newip
WSHShell.RegWrite print2 & “PortName”, “IP_” & newip
WSHShell.RegWrite print4 & “IPaddress”, newip
WSHShell.RegWrite print5 & “Port”, “IP_” & newip
WSHShell.RegWrite print6 & “PortName”, “IP_” & newip

There was 2 ways to go to fix this, either remove all the lines above except for the print4 statement, or write the registry entries to create a new printer port altogether. I decided to go with the latter as it’s better than having an incorrectly named TCP port. I don’t know if all of the entries are neccessary but I figured it better to err on the side of caution.

WSHShell.RegWrite print3 & “IP_” & newip & “\” &”HostName”, “”
WSHShell.RegWrite print3 & “IP_” & newip & “\” &”HWAddress”, “”
WSHShell.RegWrite print3 & “IP_” & newip & “\” &”IPAddress”, newip
WSHShell.RegWrite print3 & “IP_” & newip & “\” &”PortNumber”, “9100″, “REG_DWORD”
WSHShell.RegWrite print3 & “IP_” & newip & “\” &”Protocol”, “1″, “REG_DWORD”
WSHShell.RegWrite print3 & “IP_” & newip & “\” &”SNMP Community”, “public”, “REG_SZ”
WSHShell.RegWrite print3 & “IP_” & newip & “\” &”SNMP Enabled”, “1″, “REG_DWORD”
WSHShell.RegWrite print3 & “IP_” & newip & “\” &”SNMP Index”, “1″, “REG_DWORD”
WSHShell.RegWrite print3 & “IP_” & newip & “\” &”Version”, “1″, “REG_DWORD”

Although the registry is updated the printer control panels are not until after a reboot. I added a line to this echo to make sure there’s no confusion.

WScript.Echo “Printers have been updated. A reboot is required before changes will take effect.”

Here’s the entire corrected script:

‘Printer IP Migration Script by Michael Wade 7.5.06
‘based on the Printer Update script posted by Kheldaroz on http://www.experts-exchange.com/Networking/Q_20830849.html
‘See also http://www.experts-exchange.com/Networking/Q_21908953.html
‘and http://dahifi.net/index.php/archive/71/

‘First create a ‘printerip.csv’ file with newIP in 1st column and oldIP in 2nd column.

Set WshShell = WScript.CreateObject(”WScript.Shell”)
Set WshNetwork = WScript.CreateObject(”WScript.Network”)
Set Printers = WshNetwork.EnumPrinterConnections

For i = 0 to Printers.Count – 1 step 2
Set objFSO = CreateObject(”Scripting.FileSystemObject”)
Set objTextFile = objFSO.OpenTextFile (”printerip.csv”, 1)
Do Until objTextFile.AtEndOfStream
strNextLine = objTextFile.Readline
arrServiceList = Split(strNextLine , “,”)
ip = arrServiceList(1)
newip = arrServiceList(0)
if Printers.Item(i) = “IP_” & ip then
print1 = “HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Printers\” & printers.item(i+1) & “\”
print2 = print1 & “DsSpooler\”
print3 = “HKLM\system\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports\”
print4 = print3 & Printers.Item(i) & “\”
print5 = “HKLM\system\CurrentControlSet\Control\Print\Printers\” & printers.item(i+1) & “\”
print6 = print5 & “DsSpooler\”
WScript.Echo “Printer ” &Printers.Item(i+1) &” has been updated.”
WSHShell.RegWrite print1 & “Port”, “IP_” & newip
WSHShell.RegWrite print2 & “PortName”, “IP_” & newip
WSHShell.RegWrite print5 & “Port”, “IP_” & newip
WSHShell.RegWrite print6 & “PortName”, “IP_” & newip
WSHShell.RegWrite print3 & “IP_” & newip & “\” &”HostName”, “”
WSHShell.RegWrite print3 & “IP_” & newip & “\” &”HWAddress”, “”
WSHShell.RegWrite print3 & “IP_” & newip & “\” &”IPAddress”, newip
WSHShell.RegWrite print3 & “IP_” & newip & “\” &”PortNumber”, “9100″, “REG_DWORD”
WSHShell.RegWrite print3 & “IP_” & newip & “\” &”Protocol”, “1″, “REG_DWORD”
WSHShell.RegWrite print3 & “IP_” & newip & “\” &”SNMP Community”, “public”, “REG_SZ”
WSHShell.RegWrite print3 & “IP_” & newip & “\” &”SNMP Enabled”, “1″, “REG_DWORD”
WSHShell.RegWrite print3 & “IP_” & newip & “\” &”SNMP Index”, “1″, “REG_DWORD”
WSHShell.RegWrite print3 & “IP_” & newip & “\” &”Version”, “1″, “REG_DWORD”
end if
Loop
next
WScript.Echo “Printers have been updated. A reboot is required before changes will take effect.”

Leave a Reply

Your email address will not be published.