Add printer

Started by mtruong1, December 05, 2017, 09:50:29 AM

Previous topic - Next topic

mtruong1

I need to automate the process of adding an HP printer (HP Officejet 5740) onto my local laptop.  The printer has an ip address 192.168.x.x and it is not sharing on server.
what is the best way to do this? How do I specify the driver, port information of this printer?

td

You could give the Network Extender's wntAddPrinter function a try using the IP address in the "net-resource" parameter.  The IP address could be in the form '\\xxx.xxx.xxx.xxx' or just 'xxx.xxx.xxx.xxx'.  Try both and give us a report back. 
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

td

I haven't tried it but another option on newer versions of Windows is the printui.dll utility that is started via "rundll32 printui.dll,PrintUIEntry [options] [@commandfile]".  You can run printui.exe to get a dump of the options.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

td

"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

mtruong1

I tried the rundll32 printui.dll,PrintUIEntry.  specifically, Add per machine printer connection: rundll32 printui.dll,PrintUIEntry /ga  /n\\machine\printer /j"LanMan Print Services" but it did not work for me.

I saw the following command to add printer...

AddExtender("WWPRT4
4I.DLL")
pAddPrinter("", "My new printer", "LPT1:", "HP LaserJet 4", "WinPrint")

It does not have a place to insert the IP address of the printer.  so, I am not too sure if this would work.  any suggestion?


td

Our parameter for the printui.dll look a bit off.  Have you tried wntAddPrinter?   Also look at the WMI option as described in the tutorial.  WMI is a bit more work but it might be the best way to go.   
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

mtruong1

I looking at WMI and I ran the following

   The sample code adds a connection ON the local computer To a PRINTER already installed ON another computer.
      ;The following code creates the required PrintMaster object.
      oMaster = ObjectCreate("PrintMaster.PrintMaster.1")
      ;The following code adds a connection on the local computer to a printer or a share name. The printer
      ;connection is a per-user resource. The PrinterName value must use the following form: \\server\my printer.
      oMaster.PrinterConnectionAdd( PrinterName )
      oMaster = 0

it shows error 3052: Uninitialized variable or undefined function for the  oMaster = ObjectCreate("PrintMaster.PrintMaster.1")

as for the PrinterName, it must be in the form \\server\my printer,  I am not sharing this printer on the server.  What can I do?

td

You did not use WMI.  You were attempting to use an old COM based utility that has nothing to do with WMI.   I am not sure if it is possible to download that product or that it works on newer versions of Windows.

This long thread has several VBS examples that demonstrate how to install a printer using WMI.   They should be easily converted to a WinBatch script.   You can ignore the Powershell example at the top of thread as the good stuff farther down. 

https://social.technet.microsoft.com/Forums/scriptcenter/en-US/6210fa0b-0c32-4bce-a79c-dfe835922613/create-printers-in-powershell-with-wmi-win32printer-createinstance?forum=ITCG
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

td

Here is an example from the above link converted to a WIL, a.k.a, WinBatch script. The is a completely untested.

Code (winbatch) Select
strComputer = "."
strQueue = "My New Print Queue"
strDriver = "HP LaserJet 8000 Series PCL 5"
strPort = "172.19.99.2"
strComment = "Comment"
strLocation = "Trowbridge"
strShareName = "My New Print Queue Share"

objWMIService = GetObject("winmgmts:":"{impersonationLevel=impersonate,(LoadDriver)}!\\":strComputer:"\root\cimv2")

   ; Create port
objNewPort = objWMIService.Get("Win32_TCPIPPrinterPort").SpawnInstance_
objNewPort.Name = "IP_" : strPort
objNewPort.Protocol = 1
objNewPort.HostAddress = strPort
objNewPort.PortNumber = "9100"
objNewPort.SNMPEnabled = @False
objNewPort.Put_

   ; Add driver
objDriver = objWMIService.Get("Win32_PrinterDriver")
objDriver.Name = strDriver
objDriver.SupportedPlatform = "Windows NT x86"
objDriver.Version = "3"
   ; use the property below if not installed in Windows
;objInfname = "\\server\share\driver\oem.inf"
intResult = objDriver.AddPrinterDriver(objDriver)
If intResult<>0
   Pause( "Failed to add driver: ", strDriver )
   exit
End If

   ; Create queue
objPrinter = objWMIService.Get("Win32_Printer").SpawnInstance_
objPrinter.DriverName = strDriver
objPrinter.PortName   = "IP_" : strPort
objPrinter.DeviceID   = strQueue
objPrinter.Location = strLocation
objPrinter.Comment = strComment
objPrinter.Network = @True
If strShareName<>""
   objPrinter.Shared = @True
   objPrinter.ShareName = strShareName
EndIf
objPrinter.Put_
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade