Solved

vbs script need help to delete all printer to add new ones in actual printers script by gpo's

Posted on 2008-06-24
11
1,077 Views
Last Modified: 2010-04-21
Dear Experts,

I need help in order to modify my actual scrip to be available to delete all printers and publish new ones.I havo no expericence with vbs , could you provide an easy way to modify it ??
all pc's runs windows xp prof
here my actual script , thanks in advance :
scrip :


'
' Printers.vbs - Windows Logon Script.
Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection "\\data0\CanonIR"
objNetwork.AddWindowsPrinterConnection "\\data0\CommBrot"
objNetwork.AddWindowsPrinterConnection "\\data0\CommHPColor"
0
Comment
Question by:diegomirner
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 15

Expert Comment

by:fishadr
ID: 21855893
The original code is correct:
Set objNetwork = CreateObject("Wscript.Network")
objNetwork.AddWindowsPrinterConnection "\\data0\CanonIR"

You can set the default printer using the following command:
objNetwork.SetDefaultPrinter "\\data0\CanonIR"

and reomve a printer connection using this:
objNetwork.RemovePrinterConnection "\\data0\CanonIR"
0
 
LVL 15

Expert Comment

by:fishadr
ID: 21855946
You can also use the following code to list all your printers (if you want to delete the printers then you can replace the wscript.echo section with objNetwork.RemovePrinterConnection :

  Set WshNetwork = WScript.CreateObject("WScript.Network")
         Set oPrinters = WshNetwork.EnumPrinterConnections
         WScript.Echo "Network printer mappings:"
         For i = 0 to oPrinters.Count - 1 Step 2
            WScript.Echo "Port " & oPrinters.Item(i) & " = " & oPrinters.Item(i+1)
         Next
0
 

Author Comment

by:diegomirner
ID: 21863705
thanks for that , now im geting this error :
in line 3
Error: object required :'objnetwork'
code: 800A01A8

here the new scrip to delete all printers:

'
' Printers.vbs - Windows Logon Script.
objNetwork.RemovePrinterConnection "\\data0\CanonIR"
objNetwork.RemovePrinterConnection "\\data0\CommBrot"
objNetwork.RemovePrinterConnection "\\data0\CommHPColor"
objNetwork.RemovePrinterConnection "\\data0\fell1320tn"
objNetwork.RemovePrinterConnection "\\data0\FinanBrother"
objNetwork.RemovePrinterConnection "\\data0\MembBrother"
objNetwork.RemovePrinterConnection "\\data0\Finan_Jean"
objNetwork.RemovePrinterConnection "\\data0\BrotherConf"
objNetwork.RemovePrinterConnection "\\data0\BrotherHealt"
objNetwork.RemovePrinterConnection "\\data0\BrotherExecD"
objNetwork.RemovePrinterConnection "\\data0\BrotherMem2"
objNetwork.RemovePrinterConnection "\\data0\XeroxDat"
objNetwork.RemovePrinterConnection "\\data0\BrothConf2"
objNetwork.RemovePrinterConnection "\\data0\BotherHealthCare1"
objNetwork.RemovePrinterConnection "\\data0\PRINTER_14_A"


and them i will create the new one with all new path.
DO you think thats its ok ?
or should i do only one withh all inside ?
i dont know exactly manage with the deletion , should i live it until all users logon ?
could it couse error on logon if all printer has all ready deleted ?
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 15

Expert Comment

by:fishadr
ID: 21863820
You have missed settings the ObjNetwork option at the start:
Set WshNetwork = WScript.CreateObject("WScript.Network")

You may have to add the line:
On Error Resume Next

To stop error messages in relation to deleting printers that do not exist, alternatively if you ue the EnumPrinterConnections subroutine I provided this will only remove printers that are connected on the client and would  not error, it would also remove printers that are not in the remove list provided
0
 

Author Comment

by:diegomirner
ID: 21864007
i will like to use enum + with your subrutine , but i will neen an example about it becouse im lost, can you provide me and ex of the whole script ?
thanks
0
 
LVL 15

Expert Comment

by:fishadr
ID: 21864261
'==========================================================
Set objNetwork = CreateObject("Wscript.Network")
'==========================================================
' ENUMERATE PRINTER CONNECTIONS AND DELETE THEM
Set oPrinters = WshNetwork.EnumPrinterConnections
On Error Resume Next
For i = 0 to oPrinters.Count - 1 Step 2
  objNetwork.RemovePrinterConnection oPrinters.Item(i+1),true,true
Next
On Error Goto 0
'==========================================================
' ADD THE PRINTERS THAT YOU NEED
objNetwork.AddWindowsPrinterConnection "\\data0\CanonIR"
objNetwork.AddWindowsPrinterConnection "\\data0\CommBrot"
objNetwork.AddWindowsPrinterConnection "\\data0\CommHPColor"
0
 

Author Comment

by:diegomirner
ID: 21864482
i try it , but i have an error at  this line :

Set oPrinters = WshNetwork.EnumPrinterConnections

here all the script i have try , please advice if you see the error , thanks

'
' Printers.vbs - Windows Logon Script.
Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection "\\data0\CanonIR"
objNetwork.AddWindowsPrinterConnection "\\data0\CommBrot"
objNetwork.AddWindowsPrinterConnection "\\data0\CommHPColor"


'==========================================================
Set objNetwork = CreateObject("Wscript.Network")
'==========================================================
' ENUMERATE PRINTER CONNECTIONS AND DELETE THEM
Set oPrinters = WshNetwork.EnumPrinterConnections
On Error Resume Next
For i = 0 to oPrinters.Count - 1 Step 2
  objNetwork.RemovePrinterConnection oPrinters.Item(i+1),true,true
Next
On Error Goto 0
'==========================================================
' ADD THE PRINTERS THAT YOU NEED
objNetwork.AddWindowsPrinterConnection "\\data0\CanonIR"
objNetwork.AddWindowsPrinterConnection "\\data0\CommBrot"
objNetwork.AddWindowsPrinterConnection "\\data0\CommHPColor"
0
 
LVL 15

Expert Comment

by:fishadr
ID: 21864557
Sorry, replace the line:
Set oPrinters = WshNetwork.EnumPrinterConnections

with:
Set oPrinters = ObjNetwork.EnumPrinterConnections
0
 

Author Comment

by:diegomirner
ID: 21864680
exelent works , but i have a new error :

in one of the printers it gibes me :

the printer driver is unknown
code 80070705
source:null

its strange , normaly it should take the drive from theserver , any idea ?
0
 
LVL 15

Accepted Solution

by:
fishadr earned 500 total points
ID: 21864739
If you receive this error you need to follow the instructions here to add the printer via the Run32dll method:

http://support.microsoft.com/kb/297454
0
 

Author Closing Comment

by:diegomirner
ID: 31470089
well , that looks to complicate for me today , maybe tomorrow , any ways , your solution has work so thanks a lor.
Now i will add a post in order to solve an other problem regarding the same scrip , but this time i need to be sure that all rpinters will work with A4 format, houpe you there to take over of it.
thanks again
0

Featured Post

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

Question has a verified solution.

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

So you have two Windows Servers and you have a directory/folder/files on one that you'd like to mirror to the other?  You don't really want to deal with DFS or a 3rd party solution like Doubletake. You can use Robocopy from the Windows Server 200…
I've always wanted to allow a user to have a printer no matter where they login. The steps below will show you how to achieve just that. In this Article I'll show how to deploy printers automatically with group policy and then using security fil…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

631 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