Solved

Problems with printer vb script

Posted on 2006-11-08
3
327 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
[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
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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…
Choosing a core focus or particular set of features and options can be tough. To help out, we’re going to highlight a handful of things your business needs on one of your social media pages. In other words, if one of these is missing, you should imp…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

630 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