I had a sticky problem using WMI (via win32com) in Python under Windows 7. I could not 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 fine under Windows XP. But when you run the program under Windows 7, it fails:
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: a disabled firewall (without antivirus), enabled DCOM, enabled WMI, and disabled UAC. Any help would be greatly appreciated.
Note. I am using Python 2.7.1 with pywin32 build 215 under Windows 7 Ultimate x86 (and Windows XP SP3).
source share