Connecting to and Installing Network Printers


Once a printer is setup and shared on another computer (e.g. a printer server), it is fairly easy to let other computers connect to it. This will also triger the download and installation of the printer drivers. To do this programmatically, the addWindowsPrinterConnection method of the WSH Network object does the trick quite nicely. The SetDefaultPrinter method can then be used to make a printer the default printer.

		
		Set WshNetwork = WScript.CreateObject("WScript.Network")

		PrinterPath = "\\server25\Hp Laserjet 4 Marketing"

		WshNetwork.AddWindowsPrinterConnection PrinterPath
		WshNetwork.SetDefaultPrinter PrinterPath

		

Batch Processing

To batch process the installation of a given set of network printers, the preceding code could be elaborated with loops and conditions. We go for a different approach. We will assume that there is a list of printers somewhere, and we'll use a batch file to read the list and install the printers. This batchfile could be (part of) some 'baseline' workstation setup script or so.

Let's say that we list all printers to be used in the Sales Department in a file called prn_Sales.txt. Then a convenient way to set them up would be a loop such as : FOR /F %%p in ("prn_Sales.txt") do addSharedPrinter %%P

The only thing wrong here is : there is no addSharedPrinter command. So we write one in VB script - using the AddWindowsPrinterConnection method discussed before.

	
	'use as a command in batch files
	'ideally reading entries from a text file, like

	'for /f "delims=" %%p in (printers_for_given_user.txt) do cscript addgroupprinter "%%p"

	' where printers_for_given_user.txt contains a list of printers like \\server\printersharename
	'
	'	==> use 'delims' and quoted arguments to handle printer names that contain spaces
	
	' --------------------------------------------------------------------------------------

		If WScript.Arguments.Count = 1
			Set WshNetwork = WScript.CreateObject("WScript.Network")
		
			PrinterPath = WScript.Arguments.Item(0)
			WshNetwork.AddWindowsPrinterConnection PrinterPath
		
			Set WshNetwork = Nothing
	
		Else
			wscript.echo : printer name is missing
		End If

	

note the use of cscript to run the script, en the use of 'delims' and quoted arguments ( "%%p" ) to deal with printer share names that contain spaces.

Selecting and setting the default printer

It would be convenient that the list with workgroup printers for the Sales department also indicates which printer is to be the default printer for the department, and sets it when running the batch process.

Let's say we decide to indicate the default printer like this in prn_Sales.txt :


		\\neptunus\HJ LaserJet Colour
		\\neptunus\Canon Heavy Duty	(default)
		\\neptunus\Canon_OLD
		\\backup\oldprinter

	

Due to the quotes and the delims, the complete line will be passed as one argument to the addgroupprinter.vbs statement, and we can test the argument to se if it has '(default)' in it. The addgroupprinter.vbs script would then need to get rid of the 'default' piece of string and any trailing spaces and/or tabs. To accomplish that, we add a subroutine to the script :


		IsDefaultPrinter = False	

		If WScript.Arguments.Count = 1 Then
			Set WshNetwork = WScript.CreateObject("WScript.Network")
		
			PrinterPath = WScript.Arguments.Item(0)
			PrinterPath = trim (PrinterPath)

			If Right(PrinterPath,9) = "(default)" Then
				IsDefaultPrinter = True
				cleanupPrinterPath
			End If

			WshNetwork.AddWindowsPrinterConnection PrinterPath

			If IsDefaultPrinter Then
				WshNetwork.SetDefaultPrinter PrinterPath
			End If
		
			Set WshNetwork = Nothing
	
		Else
			wscript.echo : printer name is missing
		End If

	'-------------------------------------------------------------------------------------------------
		Private Sub cleanupPrinterPath

			'remove '(default)' from string
			PrinterPath = Left(PrinterPath, (Len(PrinterPath) - 9))

			'remove trailing tabs and spaces
			Do While (Right (PrinterPath,1) = chr(9) OR Right (PrinterPath,1) = chr(32))
				
				PrinterPath = Left(PrinterPath, (Len(PrinterPath) - 1))
				trim (PrinterPath)
			Loop

			'once more for good measure
			PrinterPath = trim (PrinterPath)

		End Sub

	

Alternative Sollution

It is also be possible (using 'tokens' and/or 'delims' in the FOR /F statement) to pass 2 arguments to the addGroupPrinter command - which then would need to be modified to handle 1 resuired and 1 optional (for default printer) argument. If the number of printers is quite low, it is also feasible to hard-code them in a batch file. Consider a batch file that looks like this

	
	@echo off
	REM prnFIN.bat
	REM Printers for the Financial Department 

	cscript setup\printers\addGroupPrinter.vbs "\\srv01\hplaserjet1320PCL5"     default
	cscript setup\printers\addGroupPrinter.vbs "\\pc03\FujitsuDL3700Pr_fi"
	cscript setup\printers\addGroupPrinter.vbs "\\acds1\RICOH-FI RPCS"
	

The 'addGroupPrinter.vbs' command should install the printers, and set the printer marked 'default' to be the default printer.


	'command to setup up and connect to shared printers, and optionally set a default printer
	'usage:
	'	cscript addGroupPrinters "\\server\printersharename" ["default"]
	'
	'-----------------------------------------------------------------------------------------

		IsDefaultPrinter = False	

		If WScript.Arguments.Count > 0 Then

			Set WshNetwork = WScript.CreateObject("WScript.Network")
		
			PrinterPath = WScript.Arguments.Item(0)
			PrinterPath = trim (PrinterPath)

			WshNetwork.AddWindowsPrinterConnection PrinterPath

			If WScript.Arguments.Count = 2 AND _ 
				lcase(WScript.Arguments.Item(1)) = "default" Then

				WshNetwork.SetDefaultPrinter PrinterPath

			End If
		
			Set WshNetwork = Nothing
	
		Else
			wscript.echo "printer name is missing"
			wscript.echo "usage: cscript addGroupPrinters \\server\printersharename [default]"
		End If

	

In a script such as this one, it may be wise to do some preliminary checks (is it executed by cscript ? does it have arguments - how many ? ...) and trap errors if need be. Refer to these simple vb script tricks.


Links

and take it frome there ...


Koen Noens
September 2005