Solved

Script to Delete Redirected Printers Based on Name?

Posted on 2010-08-16
17
1,516 Views
Last Modified: 2013-12-05
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.

0
Comment
Question by:Ecompro
  • 7
  • 7
  • 3
17 Comments
 
LVL 31

Expert Comment

by:DrUltima
ID: 33448182
What language do you want to use?  VBScript, PowerShell, Batch?
0
 

Author Comment

by:Ecompro
ID: 33448271
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
 
LVL 4

Expert Comment

by:Jamie_Wilson
ID: 33448342
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
 
LVL 4

Expert Comment

by:Jamie_Wilson
ID: 33448363
* don't forget to dim WSHNetwork & Set WSHNetwork = CreateObject("WScript.Network")
0
 
LVL 31

Expert Comment

by:DrUltima
ID: 33448430
And properly end the script:

WScript.Quit

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


0
 
LVL 4

Expert Comment

by:Jamie_Wilson
ID: 33448446
I wondered why that kept happening....
0
 
LVL 31

Expert Comment

by:DrUltima
ID: 33448465
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
 

Author Comment

by:Ecompro
ID: 33449045
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
Don't lose your head updating email signatures!

Do your end users still have the wrong email signature? Do email signature updates bore you or fill you with a sense of dread? You can make this a whole lot easier on yourself by trusting an Exclaimer email signature management solution. Over 50 million users do...so should you!

 
LVL 31

Expert Comment

by:DrUltima
ID: 33463717
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
 
LVL 31

Expert Comment

by:DrUltima
ID: 33463741
ALSO (sorry I missed this), you though it is not necessary, it would not hurt to DIM your WSHPrinters.
0
 

Author Comment

by:Ecompro
ID: 33469769
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
 

Author Comment

by:Ecompro
ID: 33469803
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
 

Author Comment

by:Ecompro
ID: 33503874
Are you sure the printers are a network object rather than (some other type of) system object?
0
 
LVL 31

Expert Comment

by:DrUltima
ID: 33503998
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
 

Author Comment

by:Ecompro
ID: 33504758
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
 

Accepted Solution

by:
Ecompro earned 0 total points
ID: 33504864
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
 
LVL 31

Expert Comment

by:DrUltima
ID: 33504914
See, that is what you get for answering your own Question only 11 minutes after asking.... :)  Glad you caught that.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

My previous article  (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/Server/Windows_Server_2008/A_4466-A-beginners-guide-to-installing-SCCM2007-on-Windows-2008-R2-Server.html)detailed one possible method to get SCCM 2007 installed an…
On July 14th 2015, Windows Server 2003 will become End of Support, leaving hundreds of thousands of servers around the world that still run this 12 year old operating system vulnerable and potentially out of compliance in many organisations around t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now