1

The script runs fine when run as admin on the local computer, but when deployed via GPO as a startup script gets 'access denied' from SWbemObjectEx.put_. Here's the whole script:

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

On Error Resume Next
''--- in case the driver is already there
REM *** Installs printer driver needed for Universal PS 64 Bit
Set objDriver = objWMIService.Get("Win32_PrinterDriver") 
objDriver.Name = "HP Universal Printing PS" 
objDriver.FilePath = "\\share$\Drivers\Printers\HP\HP-Universal\x64\" 
objDriver.InfName = "\\share$\Drivers\Printers\HP\HP-Universal\x64\hpcu155v.inf"
objDriver.SupportedPlatform = "Windows NT x64" 
objDriver.Version = "3" 
errResult = objDriver.AddPrinterDriver(objDriver) 

rem *** Installs a TCP/IP printer local port on a computer
Set objNewPort = objWMIService.Get _
 ("Win32_TCPIPPrinterPort").SpawnInstance_
objNewPort.Name = "IP_172.17.87.226"
objNewPort.Protocol = 1
objNewPort.HostAddress = "172.17.87.226"
objNewPort.PortNumber = "9100"
objNewPort.SNMPCommunity = "public"
objNewPort.SNMPEnabled = False
objNewPort.Put_

rem *** installs printer 
Set objPrinter = objWMIService.Get("Win32_Printer").SpawnInstance_
objPrinter.DriverName = "HP Universal Printing PS" 
objPrinter.PortName = "IP_172.17.87.226"
objPrinter.DeviceID = "HP LaserJet P2055dn in TPCS-400"
objPrinter.Shared = False
objPrinter.Location = "TPCS-400"
objPrinter.Put_

I used a second version of the script to debug by adding liberal entries like so:

writeLineToLog("addPrinterDriver errResult=" & errResult)
If Err.Number <> 0 Then
    writeLineToLog("Error: " & Err.Number)
    'writeLineToLog("Error (Hex): " & Hex(Err.Number))
    writeLineToLog("Source: " &  Err.Source)
    writeLineToLog("Description: " &  Err.Description)
    Err.Clear
End If

and another to determine the current user, here's the output of the debug:

starting script at 2/16/2017 2:41:29 PM
addPrinterDriver errResult=0
after Installs a TCP/IP printer local port on a computer
The current user is SYSTEM
after Set objPrinter
after objPrinter.DriverName =
after objPrinter.PortName =
after objPrinter.DeviceID = 
after objPrinter.Shared = 
after objPrinter.Location=
after objPrinter.Put_
after Installs printer
Error: -2147217405
Source: SWbemObjectEx
Description: Access denied 

The error is coming when the last put_ is tried.

sdjuan
  • 221
  • 2
  • 16
  • Updating the driver seemed to fix things but after a few hours it's back again, so the bounty is on again. – sdjuan Feb 22 '17 at 21:04

1 Answers1

1

Is this a user or machine GPO Policy? I would suspect if it is user that the actual user might not have permissions to add the printer.

You could try to follow this guide from MSDN's blog to give the user's access. Though I would personally recommend you deploy printers through the GPO built in functions, which you could find a guide for here.

PHELMS
  • 92
  • 2
  • 9
  • Thanks. I'm looking for why this script, which we have previously deployed successfully to more than 1000 computers, is now failing, I see many other folks having the same issue when I search but none of the solutions offered fixes the issue for us. The script came from https://gallery.technet.microsoft.com/scriptcenter/41a4c996-b7f7-4d58-808d-2acac20ddbf7 It's deployed as a machine policy. Note above that when it fails, it is running as system. – sdjuan Feb 27 '17 at 16:48
  • Also this is a standalone printer, so AFAIK can't be deployed by the method in the guide you linked to as there's no print server, plus it can't be deployed this way either https://technet.microsoft.com/en-us/library/cc754294.aspx?f=255&MSPPError=-2147217396 as it does not show in the list to be selected, so I'm stuck with deploying via script. I have a workaround which is to deploy via SCCM using the exact same script which just works, but I'd still like to know why it won't work as a GPO. – sdjuan Feb 27 '17 at 17:24