robynhowes
asked on
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.
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
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
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.
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).
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).
ASKER
I tried this but when it gets to
I tried changing this around a bit with no luck... Here is what I have so far:
strPrinterIP = arrPrinterPort(1)
, 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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Wonderful! With this we will be able to do exactly what we need!
Great. Thanks for the grade.
Rob.
Rob.
http://www.activexperts.com/activmonitor/windowsmanagement/adminscripts/printing/ports/
Regards,
Rob.