使用 WMI 远程删除 Windows 7 网络打印机驱动程序
Posted
技术标签:
【中文标题】使用 WMI 远程删除 Windows 7 网络打印机驱动程序【英文标题】:Delete Windows 7 network printer driver remotely using WMI 【发布时间】:2013-08-15 13:27:37 【问题描述】:我需要一些帮助,以便在 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
代码的失败部分是“删除网络打印机” - 部分。该脚本不会列出用户在用户配置文件中连接的网络打印机,而只列出连接到计算机配置文件的本地打印机。
【问题讨论】:
请重新阅读复制代码 sn-p 的Scripting Guy article。它明确提到objNetwork.RemovePrinterConnection strPrinter
将仅在本地计算机上工作。您需要使用objPrinter.Delete_
删除远程主机上的打印机。
@Ansgar-Wiechers :感谢您提供的信息。你是对的,它只适用于本地计算机,而这正是我想要得到的。现在 (objPrinter.Attributes And NETWORK) 永远不会为真,因此该 IF 块内的代码永远不会执行。所以我是否将 objNetwork.RemovePrinterConnection 替换为 objPrinter.Delete_
我现在更新了问题中的代码 sn-p 并删除了误导性部分。
你能列举出任何打印机吗?运行以下循环时会得到什么:For Each objPrinter In objWMIService.ExecQuery("SELECT * FROM Win32_Printer") : WScript.Echo objPrinter.Name & vbTab & objPrinter.Local & vbTab & objPrinter.Network : Next
?
运行脚本的用户似乎没有映射任何网络打印机,所以没有什么可删除的。
【参考方案1】:
要删除未登录用户的(网络)打印机连接,您需要将用户配置单元加载到注册表中并从 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
【讨论】:
完美,这似乎是解决方案,谢谢!我会在测试后立即将答案标记为解决方案以上是关于使用 WMI 远程删除 Windows 7 网络打印机驱动程序的主要内容,如果未能解决你的问题,请参考以下文章