添加打印机 VBS 脚本 SWbemObjectEx 访问被拒绝

添加打印机 VBS 脚本 SWbemObjectEx 访问被拒绝

该脚本在本地计算机上以管理员身份运行时运行良好,但通过 GPO 作为启动脚本部署时,会被 SWbemObjectEx.put_ 拒绝访问。以下是整个脚本:

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_

我使用脚本的第二个版本通过添加如下自由条目来调试:

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

另一个用于确定当前用户,这是调试的输出:

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 

当最后一次尝试 put_ 时出现错误。

答案1

这是用户还是机器 GPO 策略?我怀疑如果是用户,实际用户可能没有权限添加打印机。

你可以尝试关注本指南来自 MSDN 博客授予用户访问权限。虽然我个人建议您通过 GPO 内置功能部署打印机,但您可以找到指南在这里

相关内容