-
December 17th, 2008, 02:04 PM
#1
Registered User
vbscript to add printer
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.
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()
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?
-
December 17th, 2008, 02:19 PM
#2
Driver Terrier
The second to last post here might help
Never, ever approach a computer saying or even thinking "I will just do this quickly."
-
December 18th, 2008, 01:45 PM
#3
Registered User
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:
Code:
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:
Code:
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:
Code:
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 =)
-
December 18th, 2008, 02:09 PM
#4
Driver Terrier
The other suggestions I found was to do with WMI service, being stopped or started, the WMI files are corrupt and of course, firewalling...
Never, ever approach a computer saying or even thinking "I will just do this quickly."
-
December 18th, 2008, 02:26 PM
#5
Registered User
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.
-
December 18th, 2008, 02:36 PM
#6
Driver Terrier
Sorry I can't help further.
Never, ever approach a computer saying or even thinking "I will just do this quickly."
-
December 18th, 2008, 02:37 PM
#7
Registered User
You've been a great help thus far. Like I said, I'm sure it's something that I overlooked.
-
December 19th, 2008, 10:06 AM
#8
Registered User
I figure you have a server, so why not set up a printer queue there? Makes any future changes very, very easy.
Protected by Glock. Don't mess with me!
-
January 14th, 2009, 07:17 PM
#9
Hope this help
Originally Posted by migel628
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
-
January 15th, 2009, 03:04 AM
#10
Driver Terrier
Welcome to Windrivers Dangkhoa and thanks for the tip!
Never, ever approach a computer saying or even thinking "I will just do this quickly."
Similar Threads
-
By Yakuza389 in forum Digital Imaging
Replies: 4
Last Post: January 31st, 2006, 04:25 PM
-
By TEHendricks in forum Windows 95/98/98SE/ME
Replies: 4
Last Post: August 21st, 2005, 02:32 PM
-
By suruda1 in forum Windows XP
Replies: 7
Last Post: February 26th, 2004, 10:14 PM
-
By RIOT in forum Digital Imaging
Replies: 3
Last Post: September 25th, 2001, 05:11 PM
-
By Zealing in forum Tech-To-Tech
Replies: 0
Last Post: September 10th, 2000, 08:18 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|
Bookmarks