Windows 7 使用WMI远程删除Windows 7网络打印机驱动程序
我需要一些帮助,帮助我使用在远程计算机上具有管理员权限(提升帐户)的帐户的vbscript,远程删除Windows 7客户端计算机上的网络打印机驱动程序。问题是我无法删除用户连接的打印机。其他一切似乎都起作用了。下面是脚本的代码 该脚本可以做几件事,但最终目标是从物理上删除打印机驱动程序。当前版本的脚本失败,因为驱动程序文件正在使用中。该脚本包含避免删除特殊打印机的代码。它还会停止和启动后台打印程序Windows 7 使用WMI远程删除Windows 7网络打印机驱动程序,windows-7,printing,vbscript,wmi,driver,Windows 7,Printing,Vbscript,Wmi,Driver,我需要一些帮助,帮助我使用在远程计算机上具有管理员权限(提升帐户)的帐户的vbscript,远程删除Windows 7客户端计算机上的网络打印机驱动程序。问题是我无法删除用户连接的打印机。其他一切似乎都起作用了。下面是脚本的代码 该脚本可以做几件事,但最终目标是从物理上删除打印机驱动程序。当前版本的脚本失败,因为驱动程序文件正在使用中。该脚本包含避免删除特殊打印机的代码。它还会停止和启动后台打印程序 intSleep = 4000 strService = " 'Spoo
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
intSleep=4000
strService=“‘假脱机程序’”
strComputer=“”
Set fsobj=CreateObject(“Scripting.FileSystemObject”)”调用文件系统对象
设置objNetwork=CreateObject(“WScript.Network”)
arrPrinters=Array(“PDF”、“Adobe”、“远程”、“传真”、“Microsoft”、“发送到”、“通用”)
设置objWMIService=GetObject(“winmgmts:_
&“{impersonationLevel=impersonate}!\\”&strComputer&“\root\cimv2”)
'列出驱动程序
设置colinstalledprants=objWMIService.ExecQuery_
(“从Win32_PrinterDriver中选择*)
设置drivrutinCol=CreateObject(“Scripting.Dictionary”)
对于ColInstalledPrinter中的每个objPrinter
“Wscript.Echo”配置文件:“&objPrinter.ConfigFile”
“Wscript.Echo”数据文件:“&objPrinter.DataFile”
“Wscript.Echo”说明:“&objPrinter.Description”
'Wscript.Echo“驱动程序路径:”&objPrinter.DriverPath
'Wscript.Echo“文件路径:”&objPrinter.FilePath
'Wscript.Echo“帮助文件:”&objPrinter.HelpFile
'Wscript.Echo“INF Name:&objPrinter.InfName
'Wscript.Echo“监视器名称:”&objPrinter.MonitorName
'Wscript.Echo“Name:&objPrinter.Name
'Wscript.Echo“OEM Url:&objPrinter.oemur
'Wscript.Echo“支持的平台:”&objPrinter.SupportedPlatform
“Wscript.Echo”版本:“&objPrinter.Version”
如果InArray(objPrinter.Name,arrPrinters)=False,则
Wscript.Echo“Name:&objPrinter.Name
DRIVRUTICOL.Add DRIVRUTICOL.Count,Replace(objPrinter.ConfigFile,“C:”,“\\”&strComputer&“\C$”)
DRIVRUTICOL.Add DRIVRUTICOL.Count,Replace(objPrinter.DataFile,“C:”,“\\”&strComputer&“\C$”)
DRIVRUTICOL.Add DRIVRUTICOL.Count,Replace(objPrinter.DriverPath,“C:”,“\\”&strComputer&“\C$”)
如果结束
下一个
'删除网络打印机
常数网络=22
设置colinstalledprants=objWMIService.ExecQuery_
(“从Win32_打印机中选择*)
对于ColInstalledPrinter中的每个objPrinter
如果是objPrinter.Attributes和NETWORK,那么
'对于用户连接的网络打印机,代码永远不会出现在这里
如果结束
下一个
'停止后台打印程序服务
Set colListOfServices=objWMIService.ExecQuery_
(“从Win32_服务中选择*,其中名称=”_
&strService(“”)
对于colListOfServices中的每个objService
objService.StopService()
WSCript.Sleep intSleep
下一个
'删除驱动程序
对于i=0到drivrutinCol.Count-1
Wscript.Echo“删除驱动程序:&drivrutinCol.Item(i)
fsobj.DeleteFile(drivrutinCol.Item(i))
下一个
'启动后台打印程序服务
对于colListOfServices中的每个objService
WSCript.Sleep intSleep
objService.StartService()
下一个
函数InArray(项,myarray)
昏暗的我
对于i=0到UBound(myarray)步骤1
如果InStr(lcase(item)、lcase(myarray(i))>0,则
InArray=True
退出功能
如果结束
下一个
InArray=False
端函数
代码的失败部分是“删除网络打印机””部分。该脚本不会在用户配置文件中列出用户已连接的网络打印机,而只列出连接到计算机配置文件的本地打印机 要删除未登录用户的(网络)打印机连接,需要将用户配置单元加载到注册表中,并从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
,然后删除相应的类实例(请参阅“备注”部分):
请重新阅读从中复制该代码段的。它明确提到,
objNetwork.RemovePrinterConnection strprenter
将仅在本地计算机上工作。您需要使用objPrinter.Delete\删除远程主机上的打印机。@Ansgar Wiechers:谢谢您提供的信息。你是对的,它只在本地计算机上工作,而这正是我想要解决的问题。现在(objPrinter.Attributes和NETWORK)永远不会为true,因此IF块中的代码永远不会执行。因此,如果我用objPrinter.Delete替换objNetwork.RemovePrinterConnection并不重要。我现在已经更新了问题中的代码片段,并删除了一个误导性的部分。你能列举任何打印机吗?运行以下循环时会得到什么:对于objWMIService.ExecQuery(“从Win32_打印机中选择*):WScript.Echo objPrinter.Name&vbTab&objPrinter.Local&vbTab&objPrinter.Network:Next
?运行脚本的用户似乎没有映射任何网络打印机,所以没有什么可以删除的。很好,这似乎是解决方案,谢谢!我将在测试后尽快将答案标记为解决方案
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