Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Script to Delete Redirected Printers Based on Name?

Posted on 2010-08-16
17
Medium Priority
?
1,656 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 7
  • 3
17 Comments
 
LVL 31

Expert Comment

by:Justin Owens
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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
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:Justin Owens
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:Justin Owens
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
 
LVL 31

Expert Comment

by:Justin Owens
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:Justin Owens
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:Justin Owens
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:Justin Owens
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On a regular basis I get questions about slow RDP performance, RDP connection problems, strange errors and even BSOD, remote computers freezing or restarting after initiation of a remote session. In a lot of this cases the quick solutions made b…
While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

596 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