简体   繁体   English

使用Python的wmi模块删除网络打印机?

[英]Remove a network printer with Python's wmi module?

For starters, here's the WMI module I'm referring to . 首先, 这是我指的WMI模块

I've tried many combinations of code, and understand how to remove network printers with wmic from the command line, as well as a basic understanding on how to remove network printers with wmi in VB, etc. but I still can't figure out how to do so in this python module. 我尝试了多种代码组合,并了解了如何从命令行中使用wmic删除网络打印机,以及如何在VB中使用wmi删除网络打印机的基本理解,等等。但是我仍然不知道如何在此python模块中执行此操作。

Does anyone have experience with this? 有人对此有经验吗? I'm testing with PyDev in Eclipse, typically on a Windows 7 machine (which this program will be used for along with XP), but also on Windows 8. 我正在Eclipse中使用PyDev进行测试,通常是在Windows 7机器(此程序将与XP一起使用)上进行测试,但也在Windows 8上进行测试。

Here's some code I've tried: 这是我尝试过的一些代码:

import wmi
c = wmi.WMI ()

c.win32_printer("\\\\server\\printer").delete

And I get the following error: 我得到以下错误:

wmi.x_wmi_invalid_query: <x_wmi: Unexpected COM Error (-2147217385, 'OLE error 0x80041017', None, None)>

A friend (who probably wishes to remain unnamed) found a solution! 一个朋友(可能希望不愿透露姓名)找到了解决方案!

for printer in c.win32_printer():
    if printer.DEVICEID == "\\\\server\\printer":
    printer.delete_()

For some reason, the server name (and possibly printer name) seem to be case-sensitive, so keep an eye out for that. 由于某种原因,服务器名称(可能还有打印机名称)似乎区分大小写,因此请注意这一点。 I'd guess it's because python is case sensitive, and it's comparing via python, not WMI. 我猜这是因为python区分大小写,并且是通过python而不是WMI进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM