Deleting printers with interactive Script

This is a useful VB Script you run this as admin, or better yet, let your users run it with RunAsSPC.

Msgbox "Parsing 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
    intAnswer = _
    Msgbox("Do you want to delete" & objPrinter.name, _
        vbYesNo, "Delete Files")

If intAnswer = vbYes Then
	objPrinter.Delete_
Else
End If

Next

Msgbox "Parsing Network Printers"

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

For Each objPrinter in colInstalledPrinters
    intAnswer = _
    Msgbox("Do you want to delete" & objPrinter.name, _
        vbYesNo, "Delete Files")

If intAnswer = vbYes Then
	objPrinter.Delete_
Else
End If

Next
Msgbox "Done"

The script is in two separate parts, you you could, for example, just hack-out the bit to delete network printers and run this automatically following a change of print server, for example.

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

To call from RunAs or RunAsSPC you would need to invoke Cscript, so create a cmd or bat file with, for example:

CallMe__PromptedRemovePrinters.cmd:

cscript.exe "\\SERVERNAME\SHARENAME\scripts\PromptedRemovePrinters.vbs"

And call that either manually (right click Run As …) or by running another batch file which invokes runasspc.exe

Create SPC file:

runasspc /cryptfile:"PromptedRemovePrinters.spc" /program:"\\SERVERNAME\SHARENAME\scripts\CallMe__PromptedRemovePrinters.cmd" /domain:"DOMAINNAME" /user:"ADMINUSER" /password:"PASSWORD"

Create batch file to call the whole thing:

RunMe__PromptedRemovePrinters.cmd:

\\SERVERNAME\SHARENAME\scripts\runasspc.exe \\SERVERNAME\SHARENAME\scripts\PromptedRemovePrinters.spc

 

This entry was posted in IT Stuff. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *