Solved

Problems with printer vb script

Posted on 2006-11-08
3
319 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 500 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
A procedure for exporting installed hotfix details of remote computers using powershell
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

19 Experts available now in Live!

Get 1:1 Help Now