Question Locked

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

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

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

3 replies

by robo_dev

For starters you may want to add
Option Explicit

to make the variables more explicit??

I would guess that the problem is that the condition >0 exists all the time, no?

If InStr(objPrinter.Name, objServer) > 0

Add some statements to echo your variables so you can debug it easier.

by duscher

Thank's very much, your echo idea helped me understand that my array declaration was off.

This discussion is closed to new replies.