0

我需要一些帮助,以在Windows 7 客户端机器上使用 vbscript 远程删除网络打印机驱动程序,该帐户在远程计算机上具有管理员权限(提升的帐户)。问题是我无法删除用户已连接的已连接打印机。其他一切似乎都有效。下面是脚本的代码。

该脚本做了几件事,但最终目标是物理删除打印机驱动程序。当前版本的脚本失败,因为驱动程序文件正在使用中。该脚本包含避免删除特殊打印机的代码。它还会停止和启动打印后台处理程序。

intSleep        = 4000
strService      = " 'Spooler' "
strComputer         = "<remote computer name>"
Set fsobj       = CreateObject("Scripting.FileSystemObject") 'Calls the File System Object
Set objNetwork      = CreateObject("WScript.Network")
arrPrinters         = Array("PDF", "Adobe", "Remote", "Fax", "Microsoft", "Send To", "Generic")

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

'   List drivers
Set colInstalledPrinters =  objWMIService.ExecQuery _
    ("Select * from Win32_PrinterDriver")

Set drivrutinCol  = CreateObject("Scripting.Dictionary")

For each objPrinter in colInstalledPrinters
'    Wscript.Echo "Configuration File: " & objPrinter.ConfigFile
'    Wscript.Echo "Data File: " & objPrinter.DataFile
'    Wscript.Echo "Description: " & objPrinter.Description
'    Wscript.Echo "Driver Path: " & objPrinter.DriverPath
'    Wscript.Echo "File Path: " & objPrinter.FilePath
'    Wscript.Echo "Help File: " & objPrinter.HelpFile
'    Wscript.Echo "INF Name: " & objPrinter.InfName
'    Wscript.Echo "Monitor Name: " & objPrinter.MonitorName
'    Wscript.Echo "Name: " & objPrinter.Name
'    Wscript.Echo "OEM Url: " & objPrinter.OEMUrl
'    Wscript.Echo "Supported Platform: " & objPrinter.SupportedPlatform
'    Wscript.Echo "Version: " & objPrinter.Version

    if InArray(objPrinter.Name, arrPrinters ) = False then
        Wscript.Echo "Name: " & objPrinter.Name
        drivrutinCol.Add    drivrutinCol.Count, Replace(objPrinter.ConfigFile, "C:", "\\" & strComputer & "\c$")
        drivrutinCol.Add    drivrutinCol.Count, Replace(objPrinter.DataFile, "C:", "\\" & strComputer & "\c$")
        drivrutinCol.Add    drivrutinCol.Count, Replace(objPrinter.DriverPath, "C:", "\\" & strComputer & "\c$")
    end if

Next

'   Remove network printers
Const NETWORK = 22
Set colInstalledPrinters = objWMIService.ExecQuery _
    ("Select * From Win32_Printer")

For Each objPrinter in colInstalledPrinters 
    If objPrinter.Attributes And NETWORK Then 

         ' The code never gets here for user connected network printers

    End If
Next

'   Stop Print Spooler Service
Set colListOfServices = objWMIService.ExecQuery _
        ("Select * from Win32_Service Where Name ="_
    & strService & " ")

For Each objService in colListOfServices
    objService.StopService()
    WSCript.Sleep intSleep
Next 

'   Delete drivers
for i = 0 to drivrutinCol.Count-1
    Wscript.Echo "Deleting driver: " & drivrutinCol.Item(i)
    fsobj.DeleteFile(drivrutinCol.Item(i))
Next

'   Start Print Spooler Service
For Each objService in colListOfServices
    WSCript.Sleep intSleep
    objService.StartService()
Next 

Function InArray(item,myarray)
  Dim i
  For i=0 To UBound(myarray) Step 1
    If InStr(lcase(item), lcase(myarray(i)))>0 Then
      InArray=True
      Exit Function
    End If
  Next
  InArray=False
End Function

代码的失败部分是“删除网络打印机”部分。该脚本不会在用户配置文件中列出用户已连接的网络打印机,而仅列出连接到计算机配置文件的本地打印机。

4

1 回答 1

2

要删除未登录用户的(网络)打印机连接,您需要将用户配置单元加载到注册表中并从Printers\Connections子项中删除相应的值:

Function qq(str) : qq = Chr(34) & str & Chr(34) : End Function

Set sh = CreateObject("WScript.Shell")

username = "..."
hive = "\\" & strComputer & "\C$\Users\" & username & "\ntuser.dat"

sh.Run "reg load HKU\temp " & qq(hive), 0, True
sh.RegDelete "HKEY_USERS\temp\Printers\Connections\server,printer"
sh.Run "reg unload HKU\temp", 0, True

您需要从网络共享加载配置单元,因为与其他子命令不同load,并且unload不适用于远程注册表。


要删除打印机驱动程序(在从用户配置中删除打印机连接之后),您需要获取第SeLoadDriverPrivilege一个,然后删除相应的Win32_PrinterDriver类实例(参见“备注”部分):

objWMIService.Security_.Privileges.AddAsString "SeLoadDriverPrivilege", True

qry = "SELECT * FROM Win32_PrinterDriver"
For Each driver In objWMIService.ExecQuery(qry)
  If driver.Name = "..." Then driver.Delete_
Next
于 2013-08-20T10:06:52.167 回答