Monday, October 7, 2013

Automatically remove offline printers

I was able to figure out through experimentation that if ExtendedPrinterStatus is equal to 7, then the printer is offline.
With that info in hand, we can build a simple vb-script that will delete the printer if it is offline:
set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
set sInstalledPrinters = objWMIService.ExecQuery ("Select * from Win32_Printer where ExtendedPrinterStatus = 7")
For Each sPrinter in sInstalledPrinters
Notice the underscore in the delete method - that is needed. Just a little twist added by Microsoft to keep you on your toes. They roll that way
To call it from your script, use the cscript.exe executable. A good example would look like this:
%windir%\system32\cscript.exe \\server\path\offline_printer_cleanup.vbs
Finally - Don't leave this thing in place all the time. Once you think enough users have logged in and all is cleaned up, I would suggest removing it from their logon script.
What happens when one of your main printers goes offline?
Yep, those calls start rolling in about a missing printer.
If you are having trouble using cut and paste from the browser, here is a link to the script on our website:
Make sure to rename to .vbs after downloading.

No comments:

Post a Comment