Link to home
Start Free TrialLog in
Avatar of JeremyVetterlein
JeremyVetterleinFlag for United States of America

asked on

Requesting help with Printer installation VB Script

Hi everyone,

I'm fairly new to VB Scripting and I am trying to adapt this printer installation script to our needs. What I'm looking for the script to accomplish:

Delete all non-local printers
Install multiple printers
 - Check to see if each printer is installed first

What this script will do:

Check to see if a single printer has been installed
Check to see if there is a local default printer set, and if that printer is the document image writer.
Install the printer and set it as default depending on the previous statement results.
Show errors if applicable

Printers are all located on a local server. I have attached the original script template... I've made my own changes to this script and had some success, but at this point I need some expert direction. I don't need a line by line explanation (that would be a lot of work for you), but at least if I could look at a working script which does the things I'm looking for I can figure out why/how it works.

Thanks!!



Option Explicit 
 
' Tell WSH to resume on errors, otherwise our error handling can't do it's job 
On Error Resume Next 
 
' Dim variables 
Dim objNetwork, objWMIService, objPrinter 
Dim colInstalledPrinters 
Dim strPrinterServer, strPrinterShare, strComputerName 
Dim Return, LocalDefault, PrinterIsInstalled 
 
'######## Define Printer Configuration here ######### 
 
strPrinterServer = "" 
strPrinterShare = "" 
 
'######## Code below does not need to be modified unless logical changes are needed ######## 
 
PrinterIsInstalled = False 
    ' We will only set this to true if we can find this printer in the list 
strComputerName = "."    ' This Computer 
 
' Get WMIService so we can run WMI queries (good stuff) 
Set objWMIService = GetObject( _ 
    "winmgmts:" & "{impersonationLevel=impersonate}!\\" _ 
    & strComputerName & "\root\cimv2") 
 
' Run a WMI query to get all the objects belonging to the  
' Win32_Printer Class (all the installed printers) 
Set colInstalledPrinters =  objWMIService.ExecQuery _ 
    ("Select * from Win32_Printer") 
 
' The WMI query returns a collection (hence, the 'col' prefix),  
' so we have to loop through the objects 
For Each objPrinter in colInstalledPrinters 
    ' Now we have objects... printer objects to be exact... 
    ' Check to see if the current printer object is the default printer 
    If objPrinter.Default = "True" Then  
    ' We found the default printer, so lets see if it's a Local printer 
' (but NOT that Image Writer doohickey) 
    If objPrinter.ServerName = Null And_ 
     objPrinter.Name <> "Microsoft Office Document Image Writer" Then
        ' User has a local Default Printer, so set LocalDefault to True 
        LocalDefault = True 
    Else 
        ' User has a Network Default Printer, so set LocalDefault to False 
        LocalDefault = False 
    End If 
    End If 
 
' Lets figure out if this printer is installed already by  
' checking each printer object for a match 
    If objPrinter.ServerName = strPrinterServer And _ 
        objPrinter.ShareName = strPrinterShare Then 
    ' Printer is already installed, so set PrinterIsInstalled to True 
    PrinterIsInstalled = True 
    End If 
Next 
 
' If the Printer is not installed, install it.  If it's already installed, do nothing. 
' This also serves to allow users to choose a different default printer, as we only 
' update the default printer if this printer has not been installed before AND they 
' do not have a local default printer. 
If Not PrinterIsInstalled Then 
    ' Printer is not installed, so install it 
 
    ' Create the Network Object 
    Set objNetwork = CreateObject("WScript.Network")  
 
    ' Create a new connection to the specified Printer Path 
    objNetwork.AddWindowsPrinterConnection strPrinterServer & "\" & strPrinterShare 
 
    ' Check to see if an error was logged 
    If err.Number <> 0 Then 
        ' An error was logged, display a nice error message indicating the error number 
        WScript.Echo "Could not map " & strPrinterServer & "\" _ 
    & strPrinterShare & " [" & err.Number & "]"  
        Err.Clear 
    Else 
        ' No errors were logged, so check to see if we should make this printer the default printer 
        If Not LocalDefault Then 
            ' User does not have a local default printer, so make this the default printer 
            objNetwork.SetDefaultPrinter strPrinterServer & "\" & strPrinterShare 
        End If 
    End If 
Else 
    ' Printer is already installed, so do nothing 
End If 
 
' Good practice to clear the main objects, ESPECIALLY WMI provider objects 
Set objWMIService = Nothing 
Set objNetwork = Nothing 
 
WScript.Quit

Open in new window

Avatar of RobSampson
RobSampson
Flag of Australia image

Hi, so what's not working in that code besides this line:
    If objPrinter.ServerName = Null And_

which needs to be
    If objPrinter.ServerName = Null And _


It seems logically fine by a sight read....
Avatar of JeremyVetterlein

ASKER

Thanks for taking a look Rob. Yeah, this is actually pretty sound except I'm having trouble adapting this for multiple printers, and having it delete only network printers at the start.
This is the original template I have been tweaking prior to any changes I have made. I don't have a great grasp of vb script so I was hoping to get some tips on what to modify.
OK, to delete all network printers first, change this line:
If objPrinter.Default = "True" Then  

to this
    If objPrinter.Network = True Then
          objPrinter.Delete_
    ElseIf objPrinter.Default = "True" Then

When you say you want to add multiple printers, how do you want to identify which printer to set as the default?  One way to decide would be to just set each added printer as the default, but the have the one you actually want set to be the last one added.

Rob.
Thanks!

I actually don't mind which printer is set to default as long as it doesn't change the default if a local printer is set.

Most users have printers installed that don't exist anymore or have incorrect mappings, so we want to delete all of the network printers without changing any local printers that may be installed and then add back only the network printers that are valid. Previously we've been using batch files for everything but VB script is much more capable, so I'm trying to learn it.
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks so much Rob. This is exactly what I was looking for. I figured an array was the best bet, but wasn't certain how to have the array checked against the installed printers.

I especially appreciate the notes as I am still learning!

Regards,

Jeremy
I've requested that this question be closed as follows:

Accepted answer: 0 points for JeremyVetterlein's comment http:/Q_27482791.html#37253129

for the following reason:

Exactly what I was looking for. Thank you!
Whoops, selected my own comment instead of Rob's :-/
Sure, thanks for the grade.  If you have any questions about it, let me know.

Rob.