Python WMI 调用在 Windows 7 下不起作用
我在 Windows 7 下的 Python 中使用 WMI(通过 win32com)遇到了一个棘手的问题。我一直无法找到解决方案。
这是我的代码:
from win32com.client import GetObject
def get_printers(computer):
""" Get a list of printers from the specified computer name. """
wmiservice = GetObject(r"winmgmts:{impersonationLevel=impersonate}!\\" + computer + r"\root\cimv2")
return wmiservice.ExecQuery("Select * from Win32_Printer")
for printer in get_printers("ps2"):
print printer.Name
这在 Windows XP 下工作得很好。但如果我在 Windows 7 下运行它,就会惨遭失败:
Traceback (most recent call last):
File "C:\Python27\Lib\site-packages\Pythonwin\pywin\framework\scriptutils.py", line 325, in RunScript
exec codeObject in __main__.__dict__
File "C:\Python27\sample\temp2.py", line 8, in <module>
for printer in get_printers("ps2"):
File "C:\Python27\sample\temp2.py", line 5, in get_printers
wmiservice = GetObject(r"winmgmts:{impersonationLevel=impersonate}!\\" + computer + r"\root\cimv2")
File "C:\Python27\lib\site-packages\win32com\client\__init__.py", line 72, in GetObject
return Moniker(Pathname, clsctx)
File "C:\Python27\lib\site-packages\win32com\client\__init__.py", line 87, in Moniker
moniker, i, bindCtx = pythoncom.MkParseDisplayName(Pathname)
com_error: (-2147024891, 'Access is denied.', None, None)
我在 Win 7 中尝试了我能想到的一切:禁用防火墙(无病毒扫描程序)、确保启用 DCOM、确保启用 WMI 并禁用 UAC。任何帮助将不胜感激。
注意:我在 Windows 7 Ultimate x86(和 Windows XP SP3)下使用 Python 2.7.1 和 pywin32 build 215。
I've run into a sticky problem using WMI (via win32com) in Python under Windows 7. I haven't been able to find a resolution for this.
Here is my code:
from win32com.client import GetObject
def get_printers(computer):
""" Get a list of printers from the specified computer name. """
wmiservice = GetObject(r"winmgmts:{impersonationLevel=impersonate}!\\" + computer + r"\root\cimv2")
return wmiservice.ExecQuery("Select * from Win32_Printer")
for printer in get_printers("ps2"):
print printer.Name
This works great under Windows XP. But fails miserably if I run this under Windows 7:
Traceback (most recent call last):
File "C:\Python27\Lib\site-packages\Pythonwin\pywin\framework\scriptutils.py", line 325, in RunScript
exec codeObject in __main__.__dict__
File "C:\Python27\sample\temp2.py", line 8, in <module>
for printer in get_printers("ps2"):
File "C:\Python27\sample\temp2.py", line 5, in get_printers
wmiservice = GetObject(r"winmgmts:{impersonationLevel=impersonate}!\\" + computer + r"\root\cimv2")
File "C:\Python27\lib\site-packages\win32com\client\__init__.py", line 72, in GetObject
return Moniker(Pathname, clsctx)
File "C:\Python27\lib\site-packages\win32com\client\__init__.py", line 87, in Moniker
moniker, i, bindCtx = pythoncom.MkParseDisplayName(Pathname)
com_error: (-2147024891, 'Access is denied.', None, None)
I tried everything I can think of in Win 7: disabled firewall (no virus scanner), ensured DCOM is enabled, ensured WMI is enabled, and disabled UAC. Any help would be greately appreciated.
Note: I'm using Python 2.7.1 with pywin32 build 215, under Windows 7 Ultimate x86 (and Windows XP SP3).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事实证明,该问题与 DCOM/WMI/UAC/防火墙无关。真正令人惊讶的是,当这些故障发生时,Win 7 PC 的事件日志中根本没有任何报告。
我注意到,与最初的问题一样,来自 Win 7 的传出连接报告“访问被拒绝”。但我也注意到传入 Win 7 PC 的连接(与上面相同的 python 脚本)报告 RPC 服务器不可用。其他 PC(非 Win 7)PC 也会在事件日志中报告 kerberos 错误 (ID 4)。
问题是我们的域上的活动目录有一些奇怪的东西,特别是在这台 Win 7 PC 上。由于某种原因,活动目录错误地认为有多个具有该名称的 PC - 这是 kerberos 事件日志的来源。
有效的修复方法是:将 Win 7 PC 从域中删除,将 PC 更改为新名称,然后将 PC 放回到域中。
As it turns out, the problem was not related to DCOM/WMI/UAC/Firewall. What was really suprising was that the Win 7 PC had nothing reported in it's event log at all when these failures happened.
What I noticed was that, as in the original problem, outgoing connections from Win 7 reported Access Denied. But I also noticed that connections incoming to the Win 7 PC (same python script as above) reported the RPC Server as being unavailable. Other PC's (not the Win 7) PC would also report a kerberos error (ID 4) in the event log.
The problem turned out that there was something funky with active directory on our domain, specifically with this Win 7 PC. For some reason active directory incorrectly thought there were multiple PCs with this name - this is the source of the kerberos event log.
The fix that worked was: taking the Win 7 PC off the domain, changing the PC to a new name, then putting the PC back on the domain.
错误代码
-2147024891
是由于 DCOM 故障造成的,从 Windows Vista 连接到 WMI 开始需要更改 Windows 防火墙、用户帐户控制 (UAC) 和 DCOM 的设置,您可以阅读这些文章处理这些问题。从 Windows Vista 开始远程连接到 WMI
the error code
-2147024891
is due to a DCOM failure, starting with Windows Vista connecting to the WMI require changes to settings for Windows Firewall, User Account Control (UAC) And DCOM, you can read these articles to deal with these issues.Connecting to WMI Remotely Starting with Windows Vista