How to Remove Network Printers shared by Specific Servers using a VBS. - TechRepublic
Question
May 27, 2009 at 08:33 AM
duscher

How to Remove Network Printers shared by Specific Servers using a VBS.

by duscher . Updated 17 years, 1 month ago

Hi all,
I’m trying to create a logon script to remove printers from a users profile if the printers is shared by a specific server. i though i was doing quite well but my script is still quite buggy. at the moment it will deleate any network printer no matter what server it is hosted on and it will only delete one at a time wich is also not what i want. Once the printers have been removed i’d like to add 2 specific printers and make the first one the default.

here is the code i have so far. can anybody tell me what i’m doing wrong?

On error resume Next

Dim WshNetwork, oPrinters
Dim oServers(3)
oServers(0) = “\\Server1”
oServers(1) = “\\Server2”
oServers(2) = “\\PrintServer01”

Dim oAdd(2)
oAdd(0) = “\\PrintServer01\HPCM6040f”
oAdd(1) = “\\PrintServer01\HP LaserJet 4200 PCL 6”

Set WshNetwork = WScript.CreateObject(“WScript.Network”)
Set oPrinters = WshNetwork.EnumPrinterConnections

‘Remove Printers
For Each objPrinter in oPrinters
For Each objServer in oServers
If InStr(objPrinter.Name, objServer) > 0 then
WshNetwork.RemovePrinterConnection objPrinter
End If
Next
Next

‘Add Printers
For Each present In oAdd
WshNetwork.AddWindowsPrinterConnection present
Next

‘Set Default Printer
WshNetwork.SetDefaultPrinter oAdd(0)

wscript.quit

This discussion is locked

All Comments