Link to home
Start Free TrialLog in
Avatar of jdhackett
jdhackettFlag for Ireland

asked on

Remove Printers with Script and continue if printer doesn't exist

Hi
The login script for our users adds printers. For example:
        Dim WshNetwork
	Set WshNetwork = WScript.CreateObject("WScript.Network")
	WshNetwork.AddWindowsPrinterConnection "\\server02\printer-10"
	WshNetwork.AddWindowsPrinterConnection "\\server02\printer-20"

Open in new window


We're moving to new printers and new servers, so I wish to remove the printers.
So I tried adding the following to the script:
        Dim WshNetwork
	Set WshNetwork = WScript.CreateObject("WScript.Network")
	WshNetwork.RemovePrinterConnection "\\server02\printer-10"
	WshNetwork.RemovePrinterConnection "\\server02\printer-20"
	WshNetwork.RemovePrinterConnection "\\server02\printer-30"

Open in new window


There are about 12 printers to remove. But not all users have all the printers. Then the script will fail and not remove further printers.
For example, if a user didn't have printer-20 but did have printer-30. The script would fail before the removal of printer 30, and the user would get a message like: "Error - This network connection does not exist"

How can I change the code so all the RemovePrinterConnection lines run?
ASKER CERTIFIED SOLUTION
Avatar of Lucas
Lucas
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial