Click to See Complete Forum and Search --> : vbscript to add printer


migel628
December 17th, 2008, 01:04 PM
Let me be the first to say that I barely know vbscript, so I searched the magical interwebs and found the following script to add an IP printer to a local machine.

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()

This script seems to work fine if there was a previously added IP printer installed, however if there have been no IP printers installed, then the script bombs out on the objPrinter.Put_ command within the Add local printer section complaining about a SWbemObjectEx: Generic failure
(80041001) error. Since this is kind of defeats the purpose of using a script for IP Printer installation, I figured I'd see if anyone here had any thoughts on why this would occur?

NooNoo
December 17th, 2008, 01:19 PM
The second to last post here (http://www.pcreview.co.uk/forums/thread-3423736.php) might help

migel628
December 18th, 2008, 12:45 PM
Great suggestion, but the code in that post creates a Local Port on the system and I'm creating Standard TCP/IP Port. I whipped up quick vbscripts from both to show:

Create Local Port:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
PortIP = "192.168.100.100"
PortName = "IP_" & PortIP
strKey = "Software\Microsoft\Windows NT\CurrentVersion\Ports"
strType = "REG_SZ"

Call createport(strKey, PortName, strType)

wsh.echo "port created"

Function createport(strKey, strValue, strType)
Set objSpoolerSvc = objWMIService.Get("Win32_Service.Name='spooler'")
iReturn = objSpoolerSvc.StopService()
Const HKEY_LOCAL_MACHINE=&H80000002
Set regkey=GetObject("winmgmts:root\default:StdRegProv")
createkey=regkey.setstringvalue(HKEY_LOCAL_MACHINE ,strKey,strValue,"")
Set regkey=nothing
iReturn = objSpoolerSvc.StartService()
End Function

Create Standard TCP/IP Port:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
PortIP = "192.168.100.100"
PortName = "IP_" & PortIP
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"

I'm thinking it may have something to do with the spooler being up and running, but I'm not 100% sure. I'm currently imaging a fresh PC to test my theory. Basically, my plan is to create a function that restarts the spooler and just call that at the beginning and end of the script.

Something like this:
Function restartSpooler()
Set objSpoolerSvc = objWMIService.Get("Win32_Service.Name='spooler'")
iReturn = objSpoolerSvc.StopService()
iReturn = objSpoolerSvc.StartService()
End Function

I'll keep the community informed of my findings (in case someone else hits this). Also, if anyone has any other ideas to toss my way, then that would be great =)

NooNoo
December 18th, 2008, 01:09 PM
The other suggestions I found was to do with WMI service, being stopped or started, the WMI files are corrupt and of course, firewalling...

migel628
December 18th, 2008, 01:26 PM
I have a system w/ no IP Printer defined (fresh system) and I'm finding that it will add the TCP/IP port and the driver, but then when I go to add the printer, I get the error. However, if I go to my test system from yesterday (the one with the initial error and manually installed & removed IP Printer), I don't get the error. Both systems have the Windows Firewalls off and stopping/starting the spooler doesn't seem to help. It's just crazy, I have script to install an IP Printer, but in order to use it, I have to 1st manually install/uninstall the printer. At least now I know why I have no hair ;) I guess I'll keep truckin' along and hopefully the issue is just something overlooked.

NooNoo
December 18th, 2008, 01:36 PM
Sorry I can't help further.

migel628
December 18th, 2008, 01:37 PM
You've been a great help thus far. Like I said, I'm sure it's something that I overlooked.

CeeBee
December 19th, 2008, 09:06 AM
I figure you have a server, so why not set up a printer queue there? Makes any future changes very, very easy.

dangkhoa3000
January 14th, 2009, 06:17 PM
I have a system w/ no IP Printer defined (fresh system) and I'm finding that it will add the TCP/IP port and the driver, but then when I go to add the printer, I get the error. However, if I go to my test system from yesterday (the one with the initial error and manually installed & removed IP Printer), I don't get the error. Both systems have the Windows Firewalls off and stopping/starting the spooler doesn't seem to help. It's just crazy, I have script to install an IP Printer, but in order to use it, I have to 1st manually install/uninstall the printer. At least now I know why I have no hair ;) I guess I'll keep truckin' along and hopefully the issue is just something overlooked.

I have the same problem like you and found out the trick to solve it. I manually add the TCP/IP printer in our file server and then shares it.After that, I put in my script like that:

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

'WshNetwork.AddwindowsPrinterConnection "\\File Server\Brother MFC-9840CDW"
'***********************
Your original code will be here
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objNewPort = objWMIService.Get _
("Win32_TCPIPPrinterPort").SpawnInstance_

objNewPort.Name = "xxxxx"
objNewPort.Protocol = 1
objNewPort.HostAddress = "xxxx"
objNewPort.PortNumber = "9100"
objNewPort.SNMPEnabled = False
objNewPort.Put_

blah
blah
blah

'***********************
WshNetwork.RemovePrinterConnection "\\File Server\Brother MFC-9840CDW

And this script works fine on every machine in our organization.
Regard,
Khoa

NooNoo
January 15th, 2009, 02:04 AM
Welcome to Windrivers Dangkhoa and thanks for the tip!