Solved

Problems with printer vb script

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows 8 in a W2K Domain 7 591
Referencing a mapped drive 6 140
Windows 2000 48-bit LBA 13 49
AD account Auto logoff 1 35
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…
Adults who share images on social media aren’t the only ones who need to worry about their privacy. Our culture’s tendency to share every move and celebration affects the privacy of our children, too.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

685 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