Skip to content

Instantly share code, notes, and snippets.

@chrono-meter
Last active May 11, 2016 08:19
Show Gist options
  • Save chrono-meter/0be1c8f8e6ab308f469a to your computer and use it in GitHub Desktop.
Save chrono-meter/0be1c8f8e6ab308f469a to your computer and use it in GitHub Desktop.
#!python3
"""`wmi.py`'s querying is very slow when fetching many items (ex. Win32_NTLogEvent). There are 2 solutions:
1. Use 'wmi._wmi_namespace._raw_query'.
2. Hack 'wmi._wmi_namespace.query'.
"""
import wmi
# Use 'wmi._wmi_namespace._raw_query'.
c = wmi.WMI(privileges=['Security'])
for obj in c._raw_query('SELECT * FROM Win32_NTLogEvent'):
print(wmi._wmi_object(obj, c.Win32_NTLogEvent))
break
# Hack 'wmi._wmi_namespace.query'.
def iquery(self, wql, instance_of=None, fields=[]):
for obj in self._raw_query(wql):
yield wmi._wmi_object(obj, instance_of, fields)
iquery.__doc__ = wmi._wmi_namespace.query.__doc__
wmi._wmi_namespace.query = iquery
c = wmi.WMI(privileges=['Security'])
for obj in c.Win32_NTLogEvent():
print(obj)
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment