Click here to Skip to main content
14,364,335 members
Rate this:
Please Sign up or sign in to vote.
See more:
I am trying to make a wmi permanent event subscription with python .First creating an eventfilter,Second creating an eventconsumer and Third bind the eventconsumer to the eventfilter using this code:

What I have tried:

import wmi

Username = "username"

Password = "password"
connection = wmi.WMI("", user=Username, password=Password)

QueryLanguage = "WQL"
Name1 = "ServiceFilter"
EventNamespace = "root\cimv2"
Query = "select * from __instanceModificationEvent within 5 where targetInstance isa 'Win32_LogicalDisk'"
j =, Name=Name1, EventNamespace=EventNamespace, Query=Query)

for j in connection.__EventFilter():

Name2 = "ServiceConsumer"
Filename = "C:\\Logfile.log"
Text = "A change has occurred on the service: %TargetInstance.ProviderName%"
k =, Filename=Filename, Text=Text)

for k in connection.__EventConsumer.LogFileEventConsumer():

l =, Path), Consumer=getattr(d, Path))

for l in connection.__FilterToConsumerBinding():

but it gives me this error:

Traceback (most recent call last):
  File "C:/Users/Windows 10/PycharmProjects/ashkan/", line 21, in <module>
    k =, Filename=Filename, Text=Text)
  File "C:\Users\Windows 10\PycharmProjects\ashkan\venv\lib\site-packages\", line 1147, in __getattr__
    return getattr (self._namespace, attribute)
  File "C:\Users\Windows 10\PycharmProjects\ashkan\venv\lib\site-packages\win32com\client\", line 527, in __getattr__
    raise AttributeError("%s.%s" % (self._username_, attr))
AttributeError: <unknown>.LogFileEventConsumer

could anyone help me to fix it?
Updated 4-Nov-19 10:30am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100