I couldn't find a VBscript forum so I hope this is ok.
I am making a script that is supposed to change the port of the printer to FILE: (that's not my problem). This script is also supposed to go through the list of installed printer drivers and if there is no HP Laserjet 4050 PCL6 installed then install the driver. If it is installed then just change the driver of a specific printer to the Laserjet 4050 PCL6. The problem I have is that I get an error 87 every time I run it. I know it has something to do with the printer install. If I comment that out the whole script runs fine. Any ideas?
Here is the code:
[code]strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\" & strComputer & "
ootcimv2")
Set colPrinters = objWMIService.ExecQuery _
("Select * From Win32_Printer Where DeviceID='ProDOC Printer'")
Set colPrintdrivers = objWMIService.ExecQuery _
("Select * from Win32_PrinterDriver ")
flag = 0
'Look for laserjet driver in installed list
For Each objPrint in colPrintdrivers
If objPrint.Name <> "HP Laserjet 4050 PCL6" Then
flag = 1
End if
Next
For Each objPrinter in colPrinters
'set port to FILE:
objPrinter.PortName = "FILE:"
'install the printer driver if it wasn't found earlier
If flag = 1 then
Set objDriver = objWMIService.Get("Win32_PrinterDriver")
objWMIService.Security_.Privileges.AddAsString "SeLoadDriverPrivilege", True
objDriver.Name = "HP Laserjet 4050 PCL6"
objDriver.SupportedPlatform = "Windows NT x86"
objDriver.Version = "3"
errResult = objDriver.AddPrinterDriver(objDriver)
End If
'if the driver is installed, change the driver of the selected printer
If flag = 0 Then
objPrinter.driverName = "HP Laserjet 4050 Series PCL6"
End If
objPrinter.Put_
Next
'display results
WScript.Echo ErrResult[/code]
Comments
[code]strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\" & strComputer & "
ootcimv2")
Set colPrinters = objWMIService.ExecQuery _
("Select * From Win32_Printer Where DeviceID='ProDOC Printer'")
Set colPrintdrivers = objWMIService.ExecQuery _
("Select * from Win32_PrinterDriver ")
flag = 0
'Look for laserjet driver in installed list
For Each objPrint in colPrintdrivers
If objPrint.Name <> "HP Laserjet 4050 Series PCL6" Then
flag = 1
End if
Next
For Each objPrinter in colPrinters
'set port to FILE:
objPrinter.PortName = "FILE:"
'install the printer driver if it wasn't found earlier
If flag = 1 then
Set objDriver = objWMIService.Get("Win32_PrinterDriver")
objWMIService.Security_.Privileges.AddAsString "SeLoadDriverPrivilege", True
objDriver.Name = "HP Laserjet 4050 Series PCL6"
objDriver.SupportedPlatform = "Windows NT x86"
objDriver.Version = "3"
errResult = objDriver.AddPrinterDriver(objDriver)
End If
objPrinter.driverName = "HP Laserjet 4050 Series PCL6"
objPrinter.Put_
Next
'display results
WScript.Echo ErrResult[/code]