?
Solved

Problems with printer vb script

Posted on 2006-11-08
3
Medium Priority
?
332 Views
Last Modified: 2007-12-19
All,
I have this printer VB script that I have cobbled together. What I would like it to do, but cannot get it to do is skip a printer if it already finds it installed. It does add the printers correctly.
Also I would like to be able to use it to remove printers as they become obsolete or have been changed.
I am no scripter and just got it to work by luck as much as judgement, so you will have to explain and detail what you mean.

**************************************************************
Option explicit

On error resume next

Dim oNet
Set oNet = CreateObject("Wscript.Network")

On error resume next

mapprinter "\\ws-ukdatastore","\4050TN B Plain"
mapprinter "\\ws-ukdatastore","\4050TN B Bond"
mapprinter "\\ws-ukdatastore","\4050TN B Manual"
mapprinter "\\ws-ukdatastore","\4050TN B Letterhead"
mapprinter "\\ws-ukdatastore","\4050TN A Plain"
mapprinter "\\ws-ukdatastore","\4050TN A Bond"
mapprinter "\\ws-ukdatastore","\4050TN A Manual"
mapprinter "\\ws-ukdatastore","\4050TN A Letterhead"
mapprinter "\\ws-ukdatastore","\CanonPIX"
mapprinter "\\ws-ukdatastore","\2200DN Plain DS"
mapprinter "\\ws-ukdatastore","\pdf"
mapprinter "\\ws-ukdatastore","\2200DN Letter"
mapprinter "\\ws-ukdatastore","\2200DN Double"
mapprinter "\\ws-ukdatastore","\KM-4035-A4DoubleSided"
mapprinter "\\ws-ukdatastore","\KM-4035-A4"
mapprinter "\\ws-ukdatastore","\KM-4035-A3"

'-----------------------------------------------------

Function MapPrinter(PrintServer, Printer)
    Dim oPrinter, PrtServer, PrtName, Prt

    Prt       = PrintServer & Printer  'Strip backslashes from UNC names
    PrtServer = Left(Prt,InStr(3,Prt, "\") - 1)
    PrtServer = Right(Left(Prt,InStr(3,Prt, "\") - 1), Len(PrtServer) - 2)
    PrtName   = Right(Prt, Len(Prt) - Len(PrtServer) - 3)

    On Error Resume Next               'If printer or server dosen't exist, code continues execution
    Set oPrinter = GetObject("WinNT://" & oNet.UserDomain & "/" & PrtServer & "/" & PrtName)

    If (oPrinter.Name = "" OR oPrinter.Class <> "PrintQueue") Then
        Wscript.Echo("Printer " & PrintServer & Printer & " is not a valid network printer")
    Else
        oNet.AddWindowsPrinterConnection(PrintServer & Printer)
'        Wscript.Echo("Mapping printer to " & PrintServer & Printer)
    End If
End Function

'-----------------------------------------------------
*******************************************************************


Thanks

Mike
0
Comment
Question by:mikesjn
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
FixingStuff earned 1500 total points
ID: 17902751
This loop can conditionaly add, set default, or remove printers based on current mapped printers (WSHPrinters.item):

'' Printer connections
Set WSHPrinters = WSHNetwork.EnumPrinterConnections
For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2
                          '''To remove old printer maps
                          If Lcase(Left(WSHPrinters.Item(LOOP_COUNTER +1),9)) = "\\Server1" Then
                               WSHNetwork.RemovePrinterConnection WSHPrinters.Item(LOOP_COUNTER +1),True,True
                          End If

                          If Lcase(Left(WSHPrinters.Item(LOOP_COUNTER +1),9)) = "\\Server2" Then
                               WSHNetwork.RemovePrinterConnection WSHPrinters.Item(LOOP_COUNTER +1),True,True
                          End If
Next

'''Remove ALL old printers
'''Enumerate all printers first, after that you can select the printers you want by performing some string checks
''Set WSHPrinters = WSHNetwork.EnumPrinterConnections
''For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2
'''To remove only networked printers use this If Statement
''    If Left(WSHPrinters.Item(LOOP_COUNTER +1),2) = "\\" Then
''      WSHNetwork.RemovePrinterConnection WSHPrinters.Item(LOOP_COUNTER +1),True,True
''    End If
'''To remove all printers incuding LOCAL printers use this statement and comment out the If Statement above
'''WSHNetwork.RemovePrinterConnection WSHPrinters.Item(LOOP_COUNTER +1),True,True
''Next
''
'''Remove a specific printer
''WSHNetwork.RemovePrinterConnection "\\ServerOld\HP5si",True,True
''                                
'''Install Printers
''WSHNetwork.AddWindowsPrinterConnection "\\Server\HP5si"


The commented blocks/snipets can be used for various printer management. Hope this helps.
FS-

0
 

Author Comment

by:mikesjn
ID: 18075514
Sorry for delay, been ill for a couple of weeks and then played catchup, including dodgey ciso switch. Really have basic windows scripting. Can you show me in relation to my existing script. I cannot get to work. I either get errors or nothing when I run it.

Thanks

Mike
0
 

Author Comment

by:mikesjn
ID: 18076207
Hi,
I have been using the script below, and it works better than the original one. If you could point out any problems or enhancements. My scripting is really basic, better at bash!!

******************************************************************
On error resume next


Set objNetwork = Wscript.CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection "\\ws-ukdatastore\HP8750_A4"
objNetwork.AddWindowsPrinterConnection "\\ws-ukdatastore\HP8750_A3"
objNetwork.AddWindowsPrinterConnection "\\ws-ukdatastore\4050TN B Plain"
objNetwork.AddWindowsPrinterConnection "\\ws-ukdatastore\4050TN B Bond"
objNetwork.AddWindowsPrinterConnection "\\ws-ukdatastore\4050TN B Manual"
objNetwork.AddWindowsPrinterConnection "\\ws-ukdatastore\4050TN B Letterhead"
objNetwork.AddWindowsPrinterConnection "\\ws-ukdatastore\KM-4035-A4DoubleSided"
objNetwork.AddWindowsPrinterConnection "\\ws-ukdatastore\KM-4035-A4"
objNetwork.AddWindowsPrinterConnection "\\ws-ukdatastore\KM-4035-A3"
objNetwork.AddWindowsPrinterConnection "\\ws-ukdatastore\KM-4035-A10"


objNetwork.RemovePrinterConnection "\\ws-ukdatastore\CanonPIX"
*******************************************************

Thanks

Mike
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

864 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