Solved

Script to Delete Redirected Printers Based on Name?

Posted on 2010-08-16
17
1,549 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: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
 
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
Can’t get the mobile email signature right?

Not having any luck when trying to create an email signature for mobile devices? Does the formatting keep messing up? Make sure you have great email signatures on all devices by using Exclaimer Cloud - Signatures for Office 365.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Scenerio: You have a server running Server 2003 and have applied a retail pack of Terminal Server Licenses.  You want to change servers or your server has crashed and you need to reapply the Terminal Server Licenses. When you enter the 16-digit lic…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

863 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

23 Experts available now in Live!

Get 1:1 Help Now