Raceyman
Raceyman

Reputation: 1354

Python WMI calls not working under Windows 7

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

Upvotes: 4

Views: 5376

Answers (2)

Raceyman
Raceyman

Reputation: 1354

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.

Upvotes: 2

RRUZ
RRUZ

Reputation: 136391

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.

Upvotes: 0

Related Questions