Solved

Printer Removal Script

Posted on 2007-12-05
13
777 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
 
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
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.

 
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Printers have changed substantially in the last 30 or so years, not just in technical capabilities but in cost and usage as well.  Printers were originally used for interfacing with the operator, not necessarily for printing copy or pictures. In …
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

930 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

11 Experts available now in Live!

Get 1:1 Help Now