wmi

Windows Management Instrumentation (WMI) is Microsoft's implementation of Web-Based Enterprise Management (WBEM), an industry initiative to provide a Common Information Model (CIM) for pretty much any information about a computer system. Naturally, the Microsoft version uses DCOM as its backbone, and therefore, from the Python point of view, needs the win32 extensions from Mark Hammond, or any other piece of code which will do the same job.

Once you've got those in place, your job is almost done. WMI itself is an hierarchy of objects with a certain amount of introspection. Most of the examples you'll come across are aimed at VBS or WSH, but they translate readily enough. However... once you've finished dabbling your toes at the water's edge and start wading in to the murky waters, you discover that things aren't always quite so straightforward.

The module I've developed does a certain amount of the dirty work so that you don't have to. You always have access to the underlying WMI COM object in case you need to get your hands dirty, but I've found that for the sort of day-to-day stuff that we do here, it performs quite adequately.

(My own experience over several machines is that this step is unnecessary, but in case you're having problems...) Tony Cappellini points out that you may need to run makepy manually on one or more of the following type libraries. The module tries to use gencache.EnsureDispatch to avoid having to do this, but it doesn't always work out.

Microsoft WMI Scripting Library
WMI ADSI Extension Type Library
WMICntl Type Library

Using WMI in a thread or in a Service

WMI is a COM-based technology and so if it's running in a thread or as part of a Service (which is implicitly threaded) then you need to call CoInitialize, which can be imported from the pythoncom module. If you fail to do this then - for some bizarre reason - the error reported is "-0x7ffbfe1c - Invalid syntax"

Copyright & License?

Example

import wmi

c = wmi.WMI ()
for s in c.Win32_Service ():
  if s.State == 'Stopped':
    print s.Caption, s.State

Documentation

Tutorial covering basic and advanced usage
Russian translation of the tutorial courtesy of Alexander Ludogovski
Cookbook with a range of examples

Download

You can always access the head of the respository

8th Apr 2007: wmi.py (v1.3, zipped - tiny) - readme - changelog Generated HTML doc

28th Dec 2006: wmi.py (v1.2.1, zipped - tiny) - readme - changelog Generated HTML doc
(Bugfix and minor doc release)

8th Oct 2006: wmi.py (v1.2, zipped - tiny) - readme - changelog

What's new in 1.0 - Highlights

Installing It

Unzip to somewhere sensible (eg %TEMP%) and then just

python setup.py install

Warning - Creating a Windows service with WMI

If you want to create a Windows service using WMI, be warned that by default the service code will run in the system directory (something like c:\winnt\system32) which contains a file called wmi.dll, presumably the core windows WMI functionality. When your script executes "import wmi", Python will find the DLL and attempt to treat is as a Python module, and will fail. NB This is fixed in the most recent few versions of the pywin32 extensions.