Headmasters
asked on
Printer Removal Script
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?
ASKER
Im using as suggested
ON ERROR RESUME NEXT
Set WSHPrinters = WSHNetwork.EnumPrinterConn ections
For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2
If Left(WSHPrinters.Item(LOOP _COUNTER +1),2) = "\\" Then
WSHNetwork.RemovePrinterCo nnection 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
ON ERROR RESUME NEXT
Set WSHPrinters = WSHNetwork.EnumPrinterConn
For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2
If Left(WSHPrinters.Item(LOOP
WSHNetwork.RemovePrinterCo
(LOOP_COUNTER +1),True,True
End If
wscript.quit
However i keep getting a Scrip Error saying Expected statement. Code 800A0400, Source Compilation error
hmuser - sorry, don't know what is causing that error. I've posted another questions asking for review of this script here: https://www.experts-exchange.com/questions/23005909/Need-someone-to-review-a-script-to-delete-all-network-printers-error-pointer-to-another-question.html
We'll pick up another expert who can straighten it out for you.
We'll pick up another expert who can straighten it out for you.
I think that the step 2 gives the error (but I'm not sure)
So can you try this:
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
ASKER
I have tried that but it comes back with expected statement.
Line 6
Char 1
Error Expected Statement
Code 800A0400
Source Compilation error
Line 6
Char 1
Error Expected Statement
Code 800A0400
Source Compilation error
Do you have the variable WSHNetwork declared?
See line 2 how to declare it?
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
and also forgot (must learn to read everything.....)
add Next in the script.
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
ASKER
Im new to the scripting world sorry
Error now is Line 7
Char 3
Expected statement
800A0400
Compilation
Error now is Line 7
Char 3
Expected statement
800A0400
Compilation
it's nothing...
try this:
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
If the error is in the line 7, you need to delete some breaklines:
WSHNetwork.RemovePrinterCo nnection WSHPrinters.Item
(LOOP_COUNTER +1),True,True
Comes to be:
WSHNetwork.RemovePrinterCo nnection WSHPrinters.Item, (LOOP_COUNTER +1),True,True
WSHNetwork.RemovePrinterCo
(LOOP_COUNTER +1),True,True
Comes to be:
WSHNetwork.RemovePrinterCo
ASKER
It now runs with no errors but the network printers arent removed from the printers and faxes.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
http://www.tek-tips.com/faqs.cfm?fid=5798