I've previously used .bat scripts to install network printers, but the problem with those is that the way we had to connect to them (using a command like the following: explorer \\printserver\printername$), once installed the print queue window would always be opened for every printer that was connected to. So if we had 3 printers that had to go on each PC and that script would run on each login, those 3 printers would run through the install process & the 3 print queue windows will open up on each login as well. Even if the printers were already installed.
This script handily works around both issues: printers that are already installed are just skipped from further processing, and if missing, the installation is silent without any dialogs or windows popping up (unless if UAC is required to install a driver or some such).
So here it is for posterity (in case those forums are ever shut down – and to improve it’s discoverability – it’s quite useful). Hopefully it will help others. I've adjusted the script a little from the original here, but it's identical where it counts.
Option Explicit
On Error Resume next
Dim Printers(3), WshNetwork, objWMIService, colPrinters, bFound, InstalledPrinter, Printer
'array of network printers we need to have installed
Printers(0) = "\\printserver\HP2600"
Printers(1) = "\\printserver\HP4200"
Printers(2) = "\\printserver\Lexmark360"
Set WshNetwork = CreateObject("WScript.Network")
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
' "." = local PC, else put in PC name instead of the dot
Set colPrinters = objWMIService.ExecQuery _
("Select * From Win32_Printer Where Local = False")
'Populate Printers Collection on the PC specified above
If colPrinters.Count <> 0 Then
'loop through array of Printers we need to have installed
For each Printer In Printers
bFound = False
'loop through printers currently installed on target machine,
'compare each to the printer we need installed:
For Each InstalledPrinter In colPrinters
'.ShareName is the share ' servername for server
If Ucase(InstalledPrinter.ServerName & "\" & InstalledPrinter.ShareName) = _
Ucase(Printer) Then
bFound = True 'if Printer is already installed/found, skip to next printer
Exit For
End If
Next
If bFound = False Then 'if Printer is not installed, install it
If Printer <> "" Then '(if Printer is named/not null string)
WshNetwork.AddWindowsPrinterConnection Printer
End If
End If
Next
End If
Set WshNetwork = Nothing
Set objWMIService = Nothing
Set colPrinters = Nothing
No comments:
Post a Comment