Solved

Problems with printer vb script

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
no desktop when login to windows server 2000 7 331
Just changed my 2000 Server DCs IP now what 3 405
Data off on old Win 2000 server. 18 593
Server 2012R2 Foundation and Server 2000 3 131
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…
Giving access to ESXi shell console is always an issue for IT departments to other Teams, or Projects. We need to find a way so that teams can use ESXTOP for their POCs, or tests without giving them the access to ESXi host shell console with a root …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

839 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