Script to Delete Redirected Printers Based on Name?

ENVIRONMENT:  Very busy terminal server running Server 2003.

SITUATION:  Many users have many auto-discovered printers which map in to TS session, such as  "Auto HP LaserJet 4000N (from USERPC) in session 17".   Also, there are many printers redirected (such as Adobe PDF, eFax, XPS Document Writer, Quickbooks PDF writer, etc) from user PCs which do NOT need to be present in the RDP session.

CONDITION:  Spooler load is high, with sometimes 100 or more of these un-needed redirected printers.

NEED:  Script we can run via sceduled task every few minutes, to delete certain redirected printers by name to clean out the ones not needed.  Examples:

 -  Delete all printers whose name starts with "Auto " (to kill the redirected auto-discovered printers)
 -  Delete all printers whose name starts with "eFax 4.4 (from " 
 -  Delete all printers whose names start with "Microsoft XPS Document Writer (from "

And so forth.  Note that the "(from " is necessary so only the redirected instances are matched, not the ones installed locally on the server.

Any recommendations or sample scripts that can be run on the print spooler object would be greatly appreciated.

EcomproAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Justin OwensITIL Problem ManagerCommented:
What language do you want to use?  VBScript, PowerShell, Batch?
0
EcomproAuthor Commented:
VBScript or Batch, whichever makes the most sense from an efficiency standpoint.  The task will be launched every 5-10 minutes because these unwanted printers pile up quickly (lots of Login activity thoughout the day).

0
Jamie_WilsonCommented:
how about something like:

Set WSHPrinters = WSHNetwork.EnumPrinterConnections
For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2
'To remove only networked printers use this If Statement
    If Left(WSHPrinters.Item(LOOP_COUNTER +1),2) = "Auto" Then
      WSHNetwork.RemovePrinterConnection WSHPrinters.Item(LOOP_COUNTER +1),True,True
    End If

but then re write it to include "eFax 4.4 (from"  & "Microsoft XPS Document Writer (from "
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

Jamie_WilsonCommented:
* don't forget to dim WSHNetwork & Set WSHNetwork = CreateObject("WScript.Network")
0
Justin OwensITIL Problem ManagerCommented:
And properly end the script:

WScript.Quit

If you don't, there is the chance the scheduled task will hang.


0
Jamie_WilsonCommented:
I wondered why that kept happening....
0
Justin OwensITIL Problem ManagerCommented:
Heh... Happens to all of us until we figure it out.  In my case, it was a Batch file that didn't have an Exit at the end of it, but the same concept applies... Gets worse with Server 2008, so be careful.
0
EcomproAuthor Commented:
There was a missing NEXT statement, but no biggie.  So far here's what I have, but it's not working.  It runs from the command line with no visible errors, but it has no effect on the redirected printers.  None of the printers matching the string are deleted.

--------------------------------------------------------------------
dim WSHNetwork
Set WSHNetwork = CreateObject("WScript.Network")
Set WSHPrinters = WSHNetwork.EnumPrinterConnections
For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2
'To remove only networked printers use this If Statement
    If Left(WSHPrinters.Item(LOOP_COUNTER +1),2) = "Auto" Then
      WSHNetwork.RemovePrinterConnection WSHPrinters.Item(LOOP_COUNTER +1),True,True
    End If
Next
WScript.Quit
--------------------------------------------------------------------

I also tried matching the eFax instanes
--------------------------------------------------------------------
    If Left(WSHPrinters.Item(LOOP_COUNTER +1),2) = "eFax 4.4 (from" Then
--------------------------------------------------------------------
 but nothing happened either.

Anyone see what might be wrong?

0
Justin OwensITIL Problem ManagerCommented:
OK... Comment out the FOR loop (FOR to NEXT).

Add a line which removes a specifically named printer after the NEXT:

WSHNetwork.RemovePrinterConnection "PRINTER NAME HERE",True,True

See if that actually works.  It may be because it is a TS connection, it has to be removed differently.  This will be a good check for you.

Code should resemble below.

Justin

dim WSHNetwork
Set WSHNetwork = CreateObject("WScript.Network")
Set WSHPrinters = WSHNetwork.EnumPrinterConnections
'For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2
'To remove only networked printers use this If Statement
'    If Left(WSHPrinters.Item(LOOP_COUNTER +1),2) = "Auto" Then
'      WSHNetwork.RemovePrinterConnection WSHPrinters.Item(LOOP_COUNTER +1),True,True
'    End If
'Next

'Test to see if this works on a named printer.  Comment out when done testing.
WSHNetwork.RemovePrinterConnection "PRINTER NAME HERE",True,True

WScript.Quit

Open in new window

0
Justin OwensITIL Problem ManagerCommented:
ALSO (sorry I missed this), you though it is not necessary, it would not hurt to DIM your WSHPrinters.
0
EcomproAuthor Commented:
I used:
--------------------------------------------------------
dim WSHNetwork
dim WSHPrinters
Set WSHNetwork = CreateObject("WScript.Network")
Set WSHPrinters = WSHNetwork.EnumPrinterConnections
WSHNetwork.RemovePrinterConnection "Auto Adobe PDF on",True,True
WScript.Quit
--------------------------------------------------------
to delete all instances of redirected "Auto Adobe PDF on CLIENTPC (from SHARE) in session X".  It returned the following (see image).

I'm confident a solution is near.  What else can we try?  
error.gif
0
EcomproAuthor Commented:
Sorry, the script I ran had the comments.  The line (14) which errored out was:

WSHNetwork.RemovePrinterConnection "Auto Adobe PDF on",True,True

.... so I changed the printer name to the full name of a singluar printer instance currently mapped in.  

WSHNetwork.RemovePrinterConnection "Auto Adobe PDF on HRG (from OFFICE) in session 7",True,True

That also failed.
0
EcomproAuthor Commented:
Are you sure the printers are a network object rather than (some other type of) system object?
0
Justin OwensITIL Problem ManagerCommented:
No, I am not.  If it is detected as a local printer, then the Rundll32 command should be able to remove the connection.

See http://www.robvanderwoude.com/2kprintcontrol.php for info on how to do this.

Justin
0
EcomproAuthor Commented:
Couldn't get that to work.  I googled around some more and found some snippets that I assembled into this:
-------------------------------------------------------
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colPrinters =  objWMIService.ExecQuery _
("Select * from Win32_Printer Where DeviceID like 'Auto Adobe PDF on%'")

For Each objPrinter in colPrinters
' objPrinter.Delete
Wscript.Echo objPrinter.Name
Next

Wscript.quit
-------------------------------------------------------

The script successfully queries the redirected printers and echos the names of each printer matching the query.  That part works, so I know the query is returning exactly what we want it to find.

HOWEVER, when I uncomment objPrinter.Delete and run it again, the script errors out with the message shown here.  

I feel like we're close to a solution-- gotta be something simple to put us over the top.
errr.gif
0
EcomproAuthor Commented:
Answering my own question.

  objPrinter.Delete

must actually be written with a trailing underscore.
 
  objPrinter.Delete_

The script works great.   Even though I found the final answer via google, thanks to DrUltima for a push in the right direction.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Justin OwensITIL Problem ManagerCommented:
See, that is what you get for answering your own Question only 11 minutes after asking.... :)  Glad you caught that.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Server OS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.