VBScript Delete local Priners- Exclusions
Hi
I am creating a VBscript to delete local printers which is nice and easy. However I need to set up a list of exclusions. At the moment I have got the following which will delete all local printers except printer1.
How do I go about and add Printer2,Printer3 etc into the DeviceID <> part of the query.
Many thanks in advance
Jimmy
I am creating a VBscript to delete local printers which is nice and easy. However I need to set up a list of exclusions. At the moment I have got the following which will delete all local printers except printer1.
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * from Win32_Printer Where Network = FALSE and DeviceID <> 'Printer1'")
For Each objPrinter in colInstalledPrinters
objPrinter.Delete_
Next
How do I go about and add Printer2,Printer3 etc into the DeviceID <> part of the query.
Many thanks in advance
Jimmy
0 Comments
[ + ] Show comments
Answers (3)
Please log in to answer
Posted by:
aogilmor
13 years ago
Posted by:
anonymous_9363
13 years ago
If the list isn't too lengthy, you could use basic SQL syntax, no? Something like:
If it is lengthy, or if the names come from some other function, I'd create a variable for the SQL string, build an array of exclusions, then loop through the array, adding names to the 'OR' statement, then execute it.
Select * from Win32_Printer Where (Network = FALSE) and (DeviceID <> 'Printer1' OR DeviceID <> 'Printer2' OR DeviceID <> 'Printer3')
If it is lengthy, or if the names come from some other function, I'd create a variable for the SQL string, build an array of exclusions, then loop through the array, adding names to the 'OR' statement, then execute it.
Posted by:
elgwhoppo
13 years ago
Use inStr with the printer name in a vbscript. For example, this script sets permissions based on printer name and driver name:
On Error Resume Next
Set sho = Wscript.CreateObject("Wscript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
'********** Pull all non-networked printer information via WMI ***********
Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * from Win32_Printer Where Network = FALSE")
'********** Do action IF 1) local printer 2) NOT the UPD printer 3) if driver contains "HP Universal Printing"
For Each objPrinter in colInstalledPrinters
If NOT InStr(objPrinter.Name, "HP Universal Printing") > 0 then
IF InStr(objPrinter.DriverName, "HP Universal Printing") > 0 then
'msgbox "subinacl.exe /printer """+objPrinter.Name+""" /grant=Users=F"
sho.run "subinacl.exe /printer """+objPrinter.Name+""" /grant=Users=F"
End if
End if
Next

so that the conversation will remain readable.