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

JeremyVetterleinAsked:
Who is Participating?
 
RobSampsonConnect With a Mentor Commented:
OK.  So I've changed things around a bit.  I haven't tested it, but hopefully it will work for you.

I've added multiple printers, and used a dictionary object to set a flag for each of them to know whether they're already installed or not.

Regards,

Rob.
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 dctPrinters, arrPrinters, strPrinterServer, strPrinterShare, strComputerName 
Dim Return, LocalDefault
 
'######## Define Printer Configuration here ######### 
 
arrPrinters = Array("\\server1\printer1", "\\server1\printer2")
 
'######## Code below does not need to be modified unless logical changes are needed ######## 

Set dctPrinters = CreateObject("Scripting.Dictionary")
dctPrinters.CompareMode = vbTextCompare
' Add each printer to our dictionary object (to test for existence) and specify zero as not being installed.
' The zero will be changed to a one if the printer is found to already be installed during the loop
For Each objPrinter In arrPrinters
	dctPrinters.Add objPrinter, 0
Next
    ' 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 
LocalDefault = False
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.Network = True Then
    	' If the printer is a network printer, and is NOT in the array at the top of the code, then delete it
    	If dctPrinters.Exists(objPrinter.ServerName & "\" & objPrinter.ShareName) = False Then
    		objPrinter.Delete_
    	' Otherwise if the printer IS in the array, then flag it as already being installed
    	Else
    		dctPrinters(objPrinter.ServerName & "\" & objPrinter.ShareName) = 1
    	End If
    ElseIf 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 
	    End If 
	End If 
 Next 
 
' If the Printers in the array are not installed, install them.  If they're 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. 
For Each objPrinter In dctPrinters
	If dctPrinters(objPrinter) = 0 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 objPrinter
	 
	    ' 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 " & objPrinter & " [" & Err.Number & ": " & Err.Description & "]"  
	        Err.Clear
	    Else 
	        ' No errors were logged, so check to see if we should make this printer the default printer 
	        If LocalDefault = False Then 
	            ' User does not have a local default printer, so make this the default printer 
	            objNetwork.SetDefaultPrinter objPrinter
	        End If 
	    End If
   	Else
   		'Do nothing, printer is already installed
   	End If
Next
 
' Good practice to clear the main objects, ESPECIALLY WMI provider objects 
Set objWMIService = Nothing 
Set objNetwork = Nothing 
 
WScript.Quit

Open in new window

0
 
RobSampsonCommented:
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....
0
 
JeremyVetterleinAuthor Commented:
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.
0
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

 
RobSampsonCommented:
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.
0
 
JeremyVetterleinAuthor Commented:
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.
0
 
JeremyVetterleinAuthor Commented:
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
0
 
JeremyVetterleinAuthor Commented:
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!
0
 
JeremyVetterleinAuthor Commented:
Whoops, selected my own comment instead of Rob's :-/
0
 
RobSampsonCommented:
Sure, thanks for the grade.  If you have any questions about it, let me know.

Rob.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.