Solved

Printer Removal Script

Posted on 2007-12-05
13
776 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 12

Expert Comment

by:Kurt_Braeckmans
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
The term "Bad USB" is a buzz word that is usually used when talking about attacks on computer systems that involve USB devices. In this article, I will show what possibilities modern windows systems (win8.x and win10) offer to fight these attacks wi…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 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