Link to home
Create AccountLog in
Avatar of Fernando
FernandoFlag for Australia

asked on

VBScript to Delete printers begining with AS, AM, AB and NA...

Hi there,
I am using the code below wich is a snippet of a larger script. It basically deletes all Network Printers wich was working great until laptop users starting networking their homes and adding network printers of their own.
Is there anyway I can do the same but delete only printers that begin with AS, AM, AB and NA OR even have a text file on the server to refer to which has a list of company network printer names?
Your assistance would be great appreciated.
Dim objFileSystem, objNetwork, objShell, objLogFile, objWMIService, colInstalledPrinters, objPrinter
Dim strTemp, strIPAddress, strSubnet, strServer, strComputer
Dim arrTemp
 
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Set objNetwork = CreateObject("WScript.Network")
Set objShell = CreateObject("WScript.Shell")
 
strTemp = objShell.ExpandEnvironmentStrings("%APPDATA%")
Set objLogFile = objFileSystem.OpenTextFile(strTemp & "\logon.log", 2, True, 0)
 
strIPAddress = GetDNSAddresses()
arrTemp = Split(strIPAddress, ".")
 
strSubnet = arrTemp(0) & "." & arrTemp(1) & "." & arrTemp(2)
 
strServer = "\\AUSYDLDC01"
 
strComputer = "."
 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
 
Set colInstalledPrinters = objWMIService.ExecQuery _
    ("Select * From Win32_Printer Where Network = True")
 
For Each objPrinter in colInstalledPrinters
    objPrinter.Delete_
Next
 
MsgBOx "All Network Printers have been disconnected"

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of purplepomegranite
purplepomegranite
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Fernando

ASKER

This is perfect. Thank you!

I have run into another small issue. Each user is setup to have a code embeded into their driver. Deleting the printer and adding it again removes this setting of course. Can I right a user code to "OTHER" tab in the driver after installing it based on username? If it is possible I will post another question. The driver is for a TOSHIBA e-STUDIO2820C.

Wishful thinking?
I had this issue with a Toshiba photocopier whereby the user had to print using a code.  It can be done, though the solution is probably specific to the printer.  I basically had to write a login script to write the value to the registry - though if I remember correctly that is probably oversimplified - I seem to remember scratching my head a few times over it, but did get there in the end!