Silently delete printer by IP address if it exists

I need to be able to detect and delete a printer by IP address from a script, in order to re-add it for consistency across the network.

In this environment because of server limitations, we have to have the printer mapped by IP. We need to be able to delete and add the printer again without any popup errors. I was able to put together a script to silently delete a printer by the path, but after digging and sticking code together I was getting nowhere as far as detecting and deleting it by port.
 
Set WshNetwork = CreateObject("WScript.Network")
Set WSHPrinters = WSHNetwork.EnumPrinterConnections
PrinterPath = "\\srv-04\HP4525"
PrinterExists = False
For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2
    If WSHPrinters.Item(LOOP_COUNTER +1) = PrinterPath Then
      PrinterExists = True
    End If
Next
If PrinterExists = True Then
WshNetwork.RemovePrinterConnection "\\srv-04\HP4525"
End IF

Open in new window

I was also able to find a script that would loop through the printers and display the Port IP, which provides me with the data I need (printer port IP_192.168.1.44), but I was unable to figure out a way to extract this information to use for an if then statement to see if the ip port is mapped.
 
Set WshNetwork = WScript.CreateObject("WScript.Network")
         Set oDrives = WshNetwork.EnumNetworkDrives
         Set oPrinters = WshNetwork.EnumPrinterConnections
         WScript.Echo "Network drive mappings:"
         For i = 0 to oDrives.Count - 1 Step 2
            WScript.Echo "Drive " & oDrives.Item(i) & " = " & oDrives.Item(i+1)
         Next
         WScript.Echo 
         WScript.Echo "Network printer mappings:"
         For i = 0 to oPrinters.Count - 1 Step 2
            WScript.Echo "Port " & oPrinters.Item(i) & " = " & oPrinters.Item(i+1)
         Next

Open in new window

Ideally I would be able to provide a variable for the printer port IP for the script to check against it and delete the printer or do nothing if it does not exist.
LVL 1
robynhowesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RobSampsonCommented:
I'm not quite sure what your criteria is for determining which printer to remove, but the scripts here should get you well on the way:
http://www.activexperts.com/activmonitor/windowsmanagement/adminscripts/printing/ports/

Regards,

Rob.
Tony MassaCommented:
If you have the port (oPrinters.Item(i)), then you can insert the following code to get the IP address:

strPrinterPort = oPrinters.Item(i)
arrPrinterPort = Split(strPrinterPort, "_")
strPrinterIP = arrPrinterPort(1)
WScript.Echo strPrinterIP

Basically, the code above will split the IP_192.168.22.23 into "IP" and "192.168.22.23" values.  The script returns the second value (or IP address).
robynhowesAuthor Commented:
I tried this but when it gets to
strPrinterIP = arrPrinterPort(1)

Open in new window

, I am given an error
Subscript out of range:'[number:1] Code:800A0009
.

I tried changing this around a bit with no luck... Here is what I have so far:





Set WshNetwork = WScript.CreateObject("WScript.Network")
Set oPrinters = WshNetwork.EnumPrinterConnections
strPrinterPort = oPrinters.Item(i)
arrPrinterPort = Split(strPrinterPort, "_")
strPrinterIP = arrPrinterPort(1)
WScript.Echo strPrinterIP

Open in new window

Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

RobSampsonCommented:
OK, is this closer to what you're after?  You specify a port address, it will find the printer attached to that port, delete the printer, then delete the TCPIP port.

I have commented out the delete lines for now, just so you can see the output.

Regards,

Rob.
strPortToDelete = "172.16.1.24"
strComputer = "." 
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
Set colInstalledPrinters = objWMIService.ExecQuery("Select * from Win32_Printer") 
For Each objPrinter in colInstalledPrinters 
	If InStr(objPrinter.PortName, strPortToDelete) > 0 Then
		strPortName = objPrinter.PortName
		WScript.Echo "About to delete printer object: " & objPrinter.DeviceID
		'objPrinter.Delete_
		Set colInstalledPorts =  objWMIService.ExecQuery("Select * from Win32_TCPIPPrinterPort Where Name = '" & strPortName & "'")
		For Each objPort In colInstalledPorts
			WScript.Echo "About to delete printer port: " & objPort.Name
			objPort.Delete
		Next
	End If
Next

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
robynhowesAuthor Commented:
Wonderful! With this we will be able to do exactly what we need!
RobSampsonCommented:
Great.  Thanks for the grade.

Rob.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.