#StackBounty: #group-policy #printer #vbscript Add printer VBS Script SWbemObjectEx Access denied

Bounty: 50

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 & "rootcimv2")

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$DriversPrintersHPHP-Universalx64" 
objDriver.InfName = "\share$DriversPrintersHPHP-Universalx64hpcu155v.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.


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.