Solved

Printer Removal Script

Posted on 2007-12-05
13
780 Views
Last Modified: 2013-12-04
I need to remove all network printers from everyone's machine. I know you can script a script to add network printers. Is there a way that you can write a script on log off to remove all network printers?
0
Comment
Question by:hmuser
  • 4
  • 4
  • 2
  • +2
13 Comments
 
LVL 27

Expert Comment

by:michko
ID: 20412864
This script includes a section to disconnect network printers:
http://www.tek-tips.com/faqs.cfm?fid=5798
0
 
LVL 1

Author Comment

by:hmuser
ID: 20418341
Im using as suggested

ON ERROR RESUME NEXT
Set WSHPrinters = WSHNetwork.EnumPrinterConnections
For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2
If Left(WSHPrinters.Item(LOOP_COUNTER +1),2) = "\\" Then
WSHNetwork.RemovePrinterConnection WSHPrinters.Item
(LOOP_COUNTER +1),True,True
End If
wscript.quit

However i keep getting a Scrip Error saying Expected statement. Code 800A0400, Source Compilation error
0
 
LVL 27

Expert Comment

by:michko
ID: 20419365
hmuser - sorry, don't know what is causing that error.  I've posted another questions asking for review of this script here:  http://www.experts-exchange.com/Hardware/Peripherals/Printers_Scanners/Q_23005909.html

We'll pick up another expert who can straighten it out for you.
0
Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

 
LVL 12

Expert Comment

by:Kurt_Braeckmans
ID: 20419510
I think that the step 2 gives the error (but I'm not sure)
So can you try this:




ON ERROR RESUME NEXT
Set WSHPrinters = WSHNetwork.EnumPrinterConnections
For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 
If Left(WSHPrinters.Item(LOOP_COUNTER +1),2) = "\\" Then
WSHNetwork.RemovePrinterConnection WSHPrinters.Item
(LOOP_COUNTER +1),True,True
End If
wscript.quit

Open in new window

0
 
LVL 1

Author Comment

by:hmuser
ID: 20419676
I have tried that but it comes back with expected statement.

Line 6
Char 1
Error Expected Statement
Code 800A0400
Source Compilation error
0
 
LVL 12

Expert Comment

by:Kurt_Braeckmans
ID: 20419740
Do you have the variable WSHNetwork declared?
See line 2 how to declare it?

ON ERROR RESUME NEXT
Set WSHNetwork = CreateObject("WScript.Network")
Set WSHPrinters = WSHNetwork.EnumPrinterConnections
For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 
If Left(WSHPrinters.Item(LOOP_COUNTER +1),2) = "\\" Then
WSHNetwork.RemovePrinterConnection WSHPrinters.Item
(LOOP_COUNTER +1),True,True
End If
wscript.quit

Open in new window

0
 
LVL 12

Expert Comment

by:Kurt_Braeckmans
ID: 20419762
and also forgot (must learn to read everything.....)
add Next in the script.
ON ERROR RESUME NEXT
Set WSHNetwork = CreateObject("WScript.Network")
Set WSHPrinters = WSHNetwork.EnumPrinterConnections
For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 
 If Left(WSHPrinters.Item(LOOP_COUNTER +1),2) = "\\" Then
  WSHNetwork.RemovePrinterConnection WSHPrinters.Item
  (LOOP_COUNTER +1),True,True
 End If
next
wscript.quit

Open in new window

0
 
LVL 1

Author Comment

by:hmuser
ID: 20419791
Im new to the scripting world sorry

Error now is Line 7
Char 3
Expected statement
800A0400
Compilation
0
 
LVL 12

Expert Comment

by:Kurt_Braeckmans
ID: 20419809
it's nothing...
try this:
ON ERROR RESUME NEXT
Set WSHNetwork = CreateObject("WScript.Network")
Set WSHPrinters = WSHNetwork.EnumPrinterConnections
For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 step 2
 If Left(WSHPrinters.Item(LOOP_COUNTER +1),2) = "\\" Then
  WSHNetwork.RemovePrinterConnection 
  WSHPrinters.Item(LOOP_COUNTER +1),True,True
 End If
next
wscript.quit

Open in new window

0
 
LVL 7

Expert Comment

by:avatar-e
ID: 20419867
If the error is in the line 7, you need to delete some breaklines:

  WSHNetwork.RemovePrinterConnection WSHPrinters.Item
  (LOOP_COUNTER +1),True,True

Comes to be:
  WSHNetwork.RemovePrinterConnection WSHPrinters.Item, (LOOP_COUNTER +1),True,True
0
 
LVL 1

Author Comment

by:hmuser
ID: 20419958
It now runs with no errors but the network printers arent removed from the printers and faxes.
0
 
LVL 7

Accepted Solution

by:
avatar-e earned 100 total points
ID: 20420114
And now?
on error resume next
Dim WshNetwork, WSHPrinters
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set WSHPrinters = WshNetwork.EnumPrinterConnections
For Each Printer In WSHPrinters
If Left(printer,2) = "\\" Then
	 WshNetwork.RemovePrinterConnection printer,True,True
End If
Next

Open in new window

0
 

Expert Comment

by:venema
ID: 20909227
All solutions above did not work for me.
The only solution which worked was:

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery _
      ("Select * From Win32_Printer Where Network = True")
For Each objPrinter in colInstalledPrinters
      objPrinter.Delete_
Next
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

This is a guide to the following problem (not exclusive but here) on Windows: Users need our support and we supporters often use global administrative accounts to do this. Using these accounts safely is a real challenge. Any admin who takes se…
Security measures require Windows be logged in using Standard User login (not Administrator).  Yet, sometimes an application has to be run “As Administrator” from a Standard User login.  This paper describes how to create a shortcut icon to launch a…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

821 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