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).