VBScript to add driver and printer locally

bwiser1
bwiser1 used Ask the Experts™
on
Hi, I have an issue with adding local printers. The script creates the port if it doesn't exist, then it should copy the driver from a local machine directory, then it should install the printer by IP to the machine. It seems I am having trouble getting the driver to copy over and install the printer when I tell it to reference the directory. I am not much of a VBScript guy, but am trying to pick up on it. Any help in resolving this would be much appreciated. FYI, the scripts I have made for the groups work great when the driver is already installed locally. Thanks for everyone's help in advance.

Please see my script below:

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objNewPort = objWMIService.Get _
    ("Win32_TCPIPPrinterPort").SpawnInstance_

objNewPort.Name = "IP_169.1.1.221"
objNewPort.Protocol = 1
objNewPort.HostAddress = "IP_169.1.1.221"
objNewPort.PortNumber = "9100"
objNewPort.SNMPEnabled = False
objNewPort.Put_

Set objPrinter = objWMIService.Get("Win32_Printer").SpawnInstance_

objPrinter.DriverName = "HP LaserJet 2200 Series PCL"
objPrinter.PortName   = "IP_169.1.1.221"
objPrinter.DeviceID   = "Engineering"
objPrinter.Location = "Engineering"
objPrinter.Network = True
rem objPrinter.Shared = FALSE
rem objPrinter.ShareName = ""
objPrinter.Put_

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objNewPort = objWMIService.Get _
    ("Win32_TCPIPPrinterPort").SpawnInstance_

objNewPort.Name = "IP_169.1.1.220"
objNewPort.Protocol = 1
objNewPort.HostAddress = "169.1.1.220"
objNewPort.PortNumber = "9100"
objNewPort.SNMPEnabled = False
objNewPort.Put_

Set objPrinter = objWMIService.Get("Win32_Printer").SpawnInstance_

objPrinter.DriverName = "Canon iR C3080/3480/3580 PCL6"
objPrinter.PortName   = "IP_169.1.1.220"
objPrinter.DeviceID   = "1F Copier"
objPrinter.Location = "Sales"
objPrinter.Network = True
rem objPrinter.Shared = FALSE
rem objPrinter.ShareName = ""
objPrinter.Put_


I was told that to reference a print driver not in the .cab I could use this syntax below; but I am not sure how to tie this in. For our purposes here, let's reference a directory on the local machines on the root of C as so: C:\PrintDrivers\xxPrinterxx

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
objWMIService.Security_.Privileges.AddAsString "SeLoadDriverPrivilege", True

Set objDriver = objWMIService.Get("Win32_PrinterDriver")

objDriver.Name = "NewPrinter Model 2900"
objDriver.SupportedPlatform = "Windows NT x86"
objDriver.Version = "3"
objDriverPath = "C:\PrintDrivers\NewPrinter.dll"
objInfname = "C:\PrintDrivers\NewPrinter.inf"
intResult = objDriver.AddPrinterDriver(objDriver)

Thanks again.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Tech Lead (Windows), Endpoint Device Management Services
Commented:
Hi,

Here is a successful script I have in live service for Windows XP (not tested on Windows 7):
Set wshShell = CreateObject("WScript.Shell")
strCurrDir = Replace(WScript.ScriptFullName, WScript.ScriptName, "")
strIPAddress = "111.1.222.333"
strComputer = "."
strPrinterName = "RICOH MFP Office 2nd Floor"
strDriverName = "RICOH Aficio MP C3300 PCL 6"
strLocation = "New Office 2nd Floor"
strInfFile = strCurrDir & "OEMSETUP.INF"

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer Where PortName = 'IP_" & strIPAddress & "' ")
For Each objPrinter in colPrinters
    MsgBox "Unable to install printer, printer already found on port 'IP_" & strIPAddress & "'." & VbCrlf & VbCrlf & "Found:  " & objPrinter.DeviceID, vbExclamation + vbSystemModal, "Printer Port already assigned"
    WSCript.Quit 114001
Next

Set objNewPort = objWMIService.Get("Win32_TCPIPPrinterPort").SpawnInstance_
objNewPort.Name = "IP_" & strIPAddress
objNewPort.Protocol = 1
objNewPort.HostAddress = strIPAddress
objNewPort.PortNumber = "9100"
objNewPort.SNMPEnabled = False
objNewPort.Put_

wshShell.Run "rundll32 printui.dll,PrintUIEntry /if /b """ & strPrinterName & """ /f """ & strInfFile & """ /r ""IP_" & strIPAddress & """ /m """ & strDriverName & """", 1, True

Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer Where DeviceID = '" & strPrinterName & "' ")
For Each objPrinter in colPrinters
    objPrinter.Location = strLocation
    objPrinter.Put_
Next
WScript.Quit 0

Open in new window


As you can see I use WMI to create the port, but shell out to printui.dll to install the driver, which automatically copies the files from the source to the Windows folder.
Now if I were to take a stab at your details with my script I would try something like (change lines 12 and 45 to the actual path to the inf file itself):
Set wshShell = CreateObject("WScript.Shell")
strCurrDir = Replace(WScript.ScriptFullName, WScript.ScriptName, "")


'### Install Printer 1 ###

strIPAddress = "169.1.1.221"
strComputer = "."
strPrinterName = "Engineering - HP LJ 2200"
strDriverName = "HP LaserJet 2200 Series PCL"
strLocation = "Engineering"
strInfFile = "C:\PrintDrivers\xxPrinterxx\nameOfHPLJPrinterINfFile.inf"

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer Where PortName = 'IP_" & strIPAddress & "' ")
For Each objPrinter in colPrinters
    MsgBox "Unable to install printer, printer already found on port 'IP_" & strIPAddress & "'." & VbCrlf & VbCrlf & "Found:  " & objPrinter.DeviceID, vbExclamation + vbSystemModal, "Printer Port already assigned"
    WSCript.Quit 114001
Next

Set objNewPort = objWMIService.Get("Win32_TCPIPPrinterPort").SpawnInstance_
objNewPort.Name = "IP_" & strIPAddress
objNewPort.Protocol = 1
objNewPort.HostAddress = strIPAddress
objNewPort.PortNumber = "9100"
objNewPort.SNMPEnabled = False
objNewPort.Put_

wshShell.Run "rundll32 printui.dll,PrintUIEntry /if /b """ & strPrinterName & """ /f """ & strInfFile & """ /r ""IP_" & strIPAddress & """ /m """ & strDriverName & """", 1, True

Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer Where DeviceID = '" & strPrinterName & "' ")
For Each objPrinter in colPrinters
    objPrinter.Location = strLocation
    objPrinter.Put_
Next


'### Install Printer 2 ###

strIPAddress = "169.1.1.220"
strComputer = "."
strPrinterName = "Sales - Canon iR"
strDriverName = "Canon iR C3080/3480/3580 PCL6"
strLocation = "Sales"
strInfFile = "C:\PrintDrivers\xxPrinterxx\nameOfCanonPrinterINfFile.inf"

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer Where PortName = 'IP_" & strIPAddress & "' ")
For Each objPrinter in colPrinters
    MsgBox "Unable to install printer, printer already found on port 'IP_" & strIPAddress & "'." & VbCrlf & VbCrlf & "Found:  " & objPrinter.DeviceID, vbExclamation + vbSystemModal, "Printer Port already assigned"
    WSCript.Quit 114001
Next

Set objNewPort = objWMIService.Get("Win32_TCPIPPrinterPort").SpawnInstance_
objNewPort.Name = "IP_" & strIPAddress
objNewPort.Protocol = 1
objNewPort.HostAddress = strIPAddress
objNewPort.PortNumber = "9100"
objNewPort.SNMPEnabled = False
objNewPort.Put_

wshShell.Run "rundll32 printui.dll,PrintUIEntry /if /b """ & strPrinterName & """ /f """ & strInfFile & """ /r ""IP_" & strIPAddress & """ /m """ & strDriverName & """", 1, True

Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer Where DeviceID = '" & strPrinterName & "' ")
For Each objPrinter in colPrinters
    objPrinter.Location = strLocation
    objPrinter.Put_
Next


MsgBox "Done!"

Open in new window


Hope this helps,
Daz.

Author

Commented:
This was exactly what I was looking for! Thank you.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial