VBScript to add printer by IP?

chriscim

n00b
Joined
Aug 14, 2008
Messages
5
Hello,

I am very new to VBScript and want to create a script and add it to a GPO for our users to have a printer automatically installed and set to default upon logon.

We are running Server 2003 and client computers are all on Vista. We are connecting to the printer directly by IP (temp solution).

The drivers we want to use are currently sitting on our server at "C:\Dell\Printer\i386"
The printers IP is 192.168.1.51.

Now when you connect to the printer on a new machine by IP, the drivers are automatically installed. I'm assuming Vista has drivers already on the printer, but we have been having problems with preloaded drivers and want to use the ones on the server.

I'm assuming I have to share the folder with the drivers on it on the server for the clients to install from it.

Also, the driver is for the Dell 5210n PS and the 5310n PS. We have the 5310n PS, will it automatically load the correct drivers?

So what it comes down to, does anyone have an easy, simple script for this? Or at least be able to point me in right direction? I have been working on this for awhile and have come up with nothing. Any help is greatly appreciated. I should also mention, I am very new to this, so don't be afraid to dumb it down for me. :)
 
Try refining your google-ing to use the term "WSH" instead of "VBS". I've really only tinkered with some aspects of WSH, but this link looks promising:
http://www.vbsedit.com/scripts/printing/ports/scr_1167.asp

You may also need to adjust your GPO to ensure that users have high enough privileges to install drivers on their local machine.

HTH !!


... And welcome to the [H] community!
 
Thanks for the welcome!

The GPO currently lets users install drivers. We've actually just started using GPM (new growing startup company!) so a lot of stuff isn't quite configured yet. I'm have another computer next to me on the domain that I am using as a testbed for GPO / User policies.
 
Code:
' Add a Printer Connection


Set WshNetwork = CreateObject("WScript.Network")

WshNetwork.AddWindowsPrinterConnection "\\PrintServer1\Xerox300"
WshNetwork.SetDefaultPrinter "\\PrintServer1\Xerox300"

This is where things get a little weird...we don't have a print server set up quite yet. All the clients are connecting to the printer directly by it's IP. I've tried plugging in the IP between the quotes with no success. I've been banging my head against the desk with this one for awhile now.
 
Code:
' Add a Printer Connection


Set WshNetwork = CreateObject("WScript.Network")

WshNetwork.AddWindowsPrinterConnection "\\PrintServer1\Xerox300"
WshNetwork.SetDefaultPrinter "\\PrintServer1\Xerox300"

This is where things get a little weird...we don't have a print server set up quite yet. All the clients are connecting to the printer directly by it's IP. I've tried plugging in the IP between the quotes with no success. I've been banging my head against the desk with this one for awhile now.


Well hopefully you can peice this together:

Adding an IP:

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

objNewPort.Name = "IP_169.254.110.14"
objNewPort.Protocol = 1
objNewPort.HostAddress = "169.254.110.14"
objNewPort.PortNumber = "9999"
objNewPort.SNMPEnabled = False
objNewPort.Put_

Add a driver:
Code:
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"
objDriver.DriverPath = "C:\Scripts\NewPrinter.dll"
objDriver.Infname = "C:\Scripts\NewPrinter.inf"
intResult = objDriver.AddPrinterDriver(objDriver)

Create the printer:
Code:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

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

objPrinter.DriverName = "HP LaserJet 4000 Series PS"
objPrinter.PortName   = "IP_169.254.110.160"
objPrinter.DeviceID   = "ScriptedPrinter"
objPrinter.Location = "USA/Redmond/Building 37/Room 114"
objPrinter.Network = True
objPrinter.Put_

Its much easier if you just set a print queue on the server =)
 
We're going to go back to that eventually, but for some bizarre reason, when we have it set up as a print server, our Dell printer constantly has a "900 RIP Software error". The people at Dell don't seem to have a clue how to fix it.

I will give this script a try and let you know how it works out. Thanks a ton :)
 
H'ok!

Let me know if this looks right.

Code:
' Adding an IP

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

		objNewPort.Name = "IP_192.168.1.51"
		objNewPort.Protocol = 1
		objNewPort.HostAddress = "192.168.1.51"
		objNewPort.PortNumber = "9100"
		objNewPort.SNMPEnabled = False
		objNewPort.Put_

'Add driver

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

	Set objDriver = objWMIService.Get("Win32_PrinterDriver")

		objDriver.Name = "Dell Laser Printer 5310n PS"
		objDriver.SupportedPlatform = "Windows NT"
		objDriver.Version = "1.0"
		objDriver.DriverPath = "\\mcpitsql01\i386"
		objDriver.Infname = "\\mcpitsql01\i386\DKAAT940.inf"
	intResult = objDriver.AddPrinterDriver(objDriver)
	
'Creating printer
	Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

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

	objPrinter.DriverName = "Dell Laser Printer 5310n PS"
	objPrinter.PortName   = "IP_192.168.1.51"
	objPrinter.DeviceID   = "SharedPrinter"
	objPrinter.Location = "Main Printer"
	objPrinter.Network = True
	objPrinter.Put_

I just debugged it and got

Code:
SWbemObjectEx (42, 2) : Generic failure

Line 42 is...

Code:
objPrinter.Put_

I guess it would help a ton if I better understood how VB worked. I understand it to an extent, but some things just confuse me...like this.
 
Check to make sure the drivers are being installed.
 
H'ok!

Let me know if this looks right.

Code:
' Adding an IP

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

		objNewPort.Name = "IP_192.168.1.51"
		objNewPort.Protocol = 1
		objNewPort.HostAddress = "192.168.1.51"
		objNewPort.PortNumber = "9100"
		objNewPort.SNMPEnabled = False
		objNewPort.Put_

'Add driver

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

	Set objDriver = objWMIService.Get("Win32_PrinterDriver")

		objDriver.Name = "Dell Laser Printer 5310n PS"
		objDriver.SupportedPlatform = "Windows NT"
		objDriver.Version = "1.0"
		objDriver.DriverPath = "\\mcpitsql01\i386"
		objDriver.Infname = "\\mcpitsql01\i386\DKAAT940.inf"
	intResult = objDriver.AddPrinterDriver(objDriver)
	
'Creating printer
	Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

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

	objPrinter.DriverName = "Dell Laser Printer 5310n PS"
	objPrinter.PortName   = "IP_192.168.1.51"
	objPrinter.DeviceID   = "SharedPrinter"
	objPrinter.Location = "Main Printer"
	objPrinter.Network = True
	objPrinter.Put_

I just debugged it and got

Code:
SWbemObjectEx (42, 2) : Generic failure

Line 42 is...

Code:
objPrinter.Put_

I guess it would help a ton if I better understood how VB worked. I understand it to an extent, but some things just confuse me...like this.

My bad. I think you need to download the server 2003 resource kit and register the prnadmin.dll under c:\program files\window sresource kit\tools. So you'll want to run this remote against ll the machines unless you want to opy that dll and register it on each client pc.
 
Sorry for bringing up an old topic, but I'm hitting the same issue w/ pretty much the same script. Basically, if there has been an IP Printer installed previously then my script works, however if there has been no IP Printer previously installed, then it bombs w/ a SWbemObjectEx: Generic failure (80041001) error. I tried grabbing the prnadmin.dll from the server 2k3 resource pack, but that didn't help. I've attached my code to be safe (in case I missed something):

Code:
PrnName = "COLOR_PRN"
PrnLocation = "PRN Room"
PrnComment = "HP Color LaserJet 3600"
PrnDrv = "HP Color LaserJet 3600"
DrvPath = "HP 3600n"
'Wsh.echo DrvPath
InfPath = DrvPath & "\hpc3600e.inf"
'Wsh.echo InfPath
PortIP = "192.168.100.100"
PortName = "IP_" & PortIP

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

''''''''''''''''''''''''''
' create ip-printer-port
''''''''''''''''''''''''''
Set objNewPort = objWMIService.Get _
    ("Win32_TCPIPPrinterPort").SpawnInstance_

objNewPort.Name = PortName
objNewPort.Protocol = 1
objNewPort.HostAddress = PortIP
objNewPort.PortNumber = "9100"
objNewPort.SNMPEnabled = False
objNewPort.Put_

wsh.echo "port created"

''''''''''''''''''''''''''
' install printer driver
''''''''''''''''''''''''''
' If the driver is not signed, one cannot use WMI scripting
' to install the driver. 
' Make sure the cat file for the package is copied to the same
' location as the driver 

objWMIService.Security_.Privileges.AddAsString "SeLoadDriverPrivilege", True

Set objDriver = objWMIService.Get("Win32_PrinterDriver")

objDriver.Name = PrnDrv
objDriver.SupportedPlatform = "Windows NT x86"
objDriver.Version = "3"
objDriver.DriverPath = DrvPath
objDriver.Infname = InfPath
intResult = objDriver.AddPrinterDriver(objDriver)

wsh.echo "driver installed"

''''''''''''''''''''''''''
' Add local printer
''''''''''''''''''''''''''
Set objPrinter = objWMIService.Get("Win32_Printer").SpawnInstance_

objPrinter.DriverName = PrnDrv
objPrinter.PortName = PortName
objPrinter.DeviceID = PrnName
objPrinter.Location = PrnLocation
objPrinter.Comment = PrnComment
objPrinter.Network = True
objPrinter.Shared = False
objPrinter.Put_

wsh.echo "printer added"

'optional:
''''''''''''''''''''''''''
' Stop & Start Printer Spooler
''''''''''''''''''''''''''
'Set objSpoolerSvc = objWMIService.Get("Win32_Service.Name='spooler'")
'iReturn = objSpoolerSvc.StopService()
'iReturn = objSpoolerSvc.StartService()

If anyone has any ideas, then that would be great, b/c having script to automatically install a printer only work if it's 1st manually installed doesn't make much sense :D
 
you need to register prnadmin.dll regsvr32 <pathtodll>\prnadmin.dll.

Break the code down and see where its failing. Does it create the ip port ? does it install the driver ?
 
you need to register prnadmin.dll regsvr32 <pathtodll>\prnadmin.dll.

Break the code down and see where its failing. Does it create the ip port ? does it install the driver ?
I'm actually having the same problem, and it seems the driver isn't being created. Here's the code I'm using to install the driver:

Code:
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")


Set objNewPort = objWMIService.get("Win32_TCPIPPrinterPort").SpawnInstance_
Set objPrinter = objWMIService.Get("Win32_Printer").SpawnInstance_
objWMIService.Security_.Privileges.AddAsString "SeLoadDriverPrivilege", True
Set objDriver = objWMIService.Get("Win32_PrinterDriver")


objDriver.Name = "HP Color LaserJet CP3505 PCL 5c"
objDriver.SupportedPlatform = "Windows NT x86"
objDriver.Version = "3"
objDriver.FilePath = "\\\\domain\\SYSVOL\\domain\\install\\drivers\\PCL5\\"
objDriver.Infname =  "\\\\domain\\SYSVOL\\domain\\install\\drivers\\PCL5\\hpc3505b.inf"
'objDriver.FilePath = "\\domain\SYSVOL\domain\install\drivers\PCL5\"
'objDriver.Infname = "\\domain\SYSVOL\domain\install\drivers\PCL5\hpc3505b.inf"
'objDriver.FilePath = "C:\\printer\\drivers\\PCL5\\"
'objDriver.Infname = "C:\\printer\\drivers\\PCL5\\hpc3505b.inf"
intResult = objDriver.AddPrinterDriver(objDriver)

WScript.Echo intResult

The script outputs 87. The overall script installs the port and finally the printer. It works on a couple systems but not others; I suspect the systems that it does work on already have the driver installed.

As you can see from the script, I've tried to specify the path a couple different ways. None of them work. How can you check to see if a driver is installed? How can you delete a driver from the system?
 
I created a separate script to copy the dll to system32, then register it prior to running the printer install script. It will add the port and install the driver, but fails on the objPrinter.Put_ under the Add local printer section. However it will install if there was a previous IP Printer installation, so that's what really baffles me.
 
I think I may look into the printui.dll and see if that makes my life any easier, but I'm still wondering what the deal is. Anyway, I found some info on printui.dll here:

http://www.computerperformance.co.uk/Logon/logon_printer_computer.htm

I found a script to delete a printer, but not a specific driver (not sure if that will help you):

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

Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * from Win32_Printer where DeviceID = 'scripted_printer'")

For Each objPrinter in colInstalledPrinters
objPrinter.Delete_
Next
 
I think I may look into the printui.dll and see if that makes my life any easier, but I'm still wondering what the deal is. Anyway, I found some info on printui.dll here:

http://www.computerperformance.co.uk/Logon/logon_printer_computer.htm

I found a script to delete a printer, but not a specific driver (not sure if that will help you):

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

Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * from Win32_Printer where DeviceID = 'ScriptedPrinter'") <-- Change ScriptedPrinted to the friendly name of the printer you want to delete

For Each objPrinter in colInstalledPrinters
objPrinter.Delete_
Next


I don't like te way printui works form print queues. It adds the printer to the registry of the system so it adds the printer to the suers profile when they log in. So if the user delets it it just comes back. it has to be removed using printui.
 
I've read complaints about that w/ printui.dll and don't want to use it if I don't have to, but if I must, then I must. Ideally, we need to go w/ a print server, but we're afraid that our users are too dumb to follow the setup directions.

I don't like te way printui works form print queues. It adds the printer to the registry of the system so it adds the printer to the suers profile when they log in. So if the user delets it it just comes back. it has to be removed using printui.
 
Also, here's a script to delete a printer port (again, not sure if that will help you):

Code:
' Delete A Printer Port

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

Set colInstalledPorts =  objWMIService.ExecQuery _
    ("Select * from Win32_TCPIPPrinterPort Where Name = 'scripted_port'")

For Each objPort in colInstalledPorts 
    objPort.Delete
Next
 
I've read complaints about that w/ printui.dll and don't want to use it if I don't have to, but if I must, then I must. Ideally, we need to go w/ a print server, but we're afraid that our users are too dumb to follow the setup directions.

setup directions ? what is differnt with using a local IP printer ?
 
Our users move rooms each year and we're never given their updated room information, so if we went w/ a print server, then we'd need to have each user connect manually. The other downside to the print server is that it's per user account, whereas using a script gives a common printer for the machine.

setup directions ? what is differnt with using a local IP printer ?
 
Our users move rooms each year and we're never given their updated room information, so if we went w/ a print server, then we'd need to have each user connect manually. The other downside to the print server is that it's per user account, whereas using a script gives a common printer for the machine.

if you went with a print server, the logon script would be MUCH easier, and you wouldn't have to fight with drivers since the machines would automatically receive the drivers from the printserver when necessary, and you could have all printers added to all the machines. That way the only "user" interaction that would be required is setting the default printer.

by doing IP printing, the print drivers must be manually installed on every machine, unless of course Windows already has the drivers. Not to metion that by doing IP printing, one machine could hose up all machines from being able to print.
 
I am able to get an IP Printer to install by naming the directory and the Drivers that I put there manually, but I'm needing to be able to have any user run this script, and it auto installs printer from the OS's list of printers. Does anyone know if this is possible, and if so, what commands to use?
 
Back
Top