[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 246
  • Last Modified:

Remove all Local Printers Except for ......

Here is my script it is tested to remove EVRYTHING LOCAL.

Can someone give me a line of code that would EXCLUDE a specific printers
....for example a printer that contains "PDF" or "XPS" or "Office"

'This removes all Local Printers

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colInstalledPrinters =  objWMIService.ExecQuery _
    ("Select * from Win32_Printer Where Network = FALSE")

For Each objPrinter in colInstalledPrinters
    objPrinter.Delete_
Next

0
dirkdigs
Asked:
dirkdigs
  • 3
  • 2
1 Solution
 
Lukasz ChmielewskiCommented:
seems like sth like this (for searching PDF in Caption value for Printer)


strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
 
Set colInstalledPrinters =  objWMIService.ExecQuery _
    ("Select * from Win32_Printer Where Network = FALSE")
 
For Each objPrinter in colInstalledPrinters
If InStr(objPrinter.Caption, "PDF") = 0 Then
    objPrinter.Delete_
else
	' do nothing - the printer string found
End If
Next

Open in new window

0
 
dirkdigsAuthor Commented:
Wil this work if the word "PDF" is found Anywhere in the printers name?
0
 
Lukasz ChmielewskiCommented:
to be precise: it would work for the printers where the caption value for win32_printers contains PDF string. so yes.

as to be more precise:
http://msdn.microsoft.com/en-us/library/aa394363(VS.85).aspx

it should rather be name value (below snippet) so this is correction:

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
 
Set colInstalledPrinters =  objWMIService.ExecQuery _
    ("Select * from Win32_Printer Where Network = FALSE")
 
For Each objPrinter in colInstalledPrinters
If InStr(objPrinter.Name, "PDF") = 0 Then
    objPrinter.Delete_
else
        ' do nothing - the printer string found
End If
Next

Open in new window

0
 
dirkdigsAuthor Commented:
i would like to purge the print queue before deleting., where do i add that in just before objPrinter.Delete_
 ?
0
 
Lukasz ChmielewskiCommented:
this is the code for cancelling all the jobs for "HP LaserJet 1018" name

download wmi code creator, migh be a handy one
http://www.microsoft.com/downloads/details.aspx?FamilyID=2cc30a64-ea15-4661-8da4-55bbc145c30e&displaylang=en


strComputer = "." 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
' Obtain an instance of the the class 
' using a key property value.
Set objShare = objWMIService.Get("Win32_Printer.DeviceID='HP LaserJet 1018'")
 
' no InParameters to define
 
' Execute the method and obtain the return status.
' The OutParameters object in objOutParams
' is created by the provider.
Set objOutParams = objWMIService.ExecMethod("Win32_Printer.DeviceID='HP LaserJet 1018'", "CancelAllJobs")
 
' List OutParams
Wscript.Echo "Out Parameters: "
Wscript.echo "ReturnValue: " & objOutParams.ReturnValue

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now