Python WMI does not work under Windows 7

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

+4
source share
2 answers

As it turns out, the problem is not related to DCOM / WMI / UAC / Firewall. What was really amazing was that on the Win 7 PC nothing was reported in the event log when these failures occurred.

I noticed that, as in the original problem, outgoing connections from Win 7 were reported by Access Denied. But I also noticed that the connections included in Win 7 PC (the same python script as above) reported that the RPC server was unavailable. Another PC (not Win 7) PC will also report a kerberos (ID 4) error in the event log.

The problem turned out to be that in our domain there was something funny with the active directory, in particular with this Win 7 computer. For some reason, the active directory incorrectly thought about the presence of several computers with this name - this is the source of the kerberos event log.

The fix that worked was that you removed the Win 7 computer from the domain, changing the computer to a new name, and then returning the PC to the domain.

+1
source

error code -2147024891 caused by DCOM failure, starting with Windows Vista connecting to WMI, you need to change the settings for Windows Firewall, User Account Control (UAC) and DCOM, you can read these articles to deal with these issues.

0
source

Source: https://habr.com/ru/post/1339389/


All Articles