使用python wmi模块创建wmi永久事件订阅时如何连接到LogFileEventConsumer类

时间:2019-11-03 17:49:47

标签: python events wmi subscription

我正在尝试使用python进行wmi永久事件订阅。首先创建一个事件过滤器,其次创建一个eventconsumer,然后使用以下代码将eventconsumer绑定到eventfilter:

import wmi

Username = "username"

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

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

for j in connection.__EventFilter():
    j.Put_()
    j.Path()

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

for k in connection.__EventConsumer.LogFileEventConsumer():
    k.Put()
    k.Path()

l = connection.__FilterToConsumerBinding.new(Filter=getattr(a, Path), Consumer=getattr(d, Path))

for l in connection.__FilterToConsumerBinding():
    l.Put()
    l.Path()

但是它给了我这个错误:

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

有人可以帮我修复它吗?

0 个答案:

没有答案