Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

VBscript install printer driver

flatleyldflatleyld Posts: 10Member
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

  • flatleyldflatleyld Posts: 10Member
    Here's an update. I found out after tinkering that the driver name of my HP Laserjet 4050 PCL6 is wrong. I've tested this script with the laserjet 4 driver and that works. Does anyone know the proper name for the laserjet 4050 PCL6?
  • flatleyldflatleyld Posts: 10Member
    Looks like I solved my own problem. Here is my finished working 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 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]
Sign In or Register to comment.