Skip to main content

Windows Management Infrastructure API for Python.

Project description

This project provides a Python native module wrapper over the Windows Management Infrastructure (MI) API [1].

Works with Python 2.7 and 3.x on any Windows version which supports the MI API, both x86 and x64.

It includes also a drop-in replacement for the Python WMI [2] module, proving much faster execution times and no dependency on pywin32.

Installation

Pip is the preferred way to install PyMI. Pre-compiled binary wheels are available on Pypi [3]:

pip install PyMI

Usage

This project can be used either with a lower level interface that reflects the underlying MI API structure or with the higher level (and slightly slower) WMI module replacement.

MI module basic usage

Here’s a simple example which enumerates all processes and kills any instance of “KillerRabbitOfCaerbannog.exe”.

import mi

with mi.Application() as a:
    with a.create_session(protocol=mi.PROTOCOL_WMIDCOM) as s:
        proc_name = u'notepad.exe'
        with s.exec_query(
                u"root\\cimv2", u"select * from Win32_Process") as q:
            i = q.get_next_instance()
            while i is not None:
                if i[u'name'].lower() == u"KillerRabbitOfCaerbannog.exe":
                    cls = i.get_class()
                    # Prepare parameters
                    params = a.create_method_params(cls, u"Terminate")
                    # Exit code
                    params['reason'] = 10
                    # Invoke method
                    with s.invoke_method(i, u"Terminate", params) as op:
                        op.get_next_instance()
                i = q.get_next_instance()

WMI module basic usage

And here’s the same example written using the WMI module replacement, which provides a simpler and higher level interface over the mi API:

import wmi

conn = wmi.WMI()
for p in conn.Win32_Process():
    if p.Name == u"KillerRabbitOfCaerbannog.exe":
        p.Terminate(reason=10)

Build

Use the following to build Python 3 wheels. Those will be copied to the build dir.

python setup.py bdist_wheel

The best way to build PyMI for Python 2.7 or 3.4 and below is to use the Visual Studio solution (described below). This will statically link the vc140 runtime, which is required by PyMI.

Custom VS env vars

distutils will automatically locate your Visual Studio and Windows SDK installation. If you’d like to call vcvarsall.bat yourself and use a specific version, use the following:

function SetVCVars($vcvarsdir, $platform="amd64")
{
    pushd $vcvarsdir
    try
    {
        cmd /c "vcvarsall.bat $platform & set" |
        foreach {
          if ($_ -match "=") {
            $v = $_.split("="); set-item -force -path "ENV:\$($v[0])"  -value "$($v[1])"
          }
        }
    }
    finally
    {
        popd
    }
}

# Replace this folder with the one in which the vcvarsall.bat script is
# located (the exact location depends on the Visual Studio version).
# SetVCVars "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build"
SetVCVars "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC"

$env:DISTUTILS_USE_SDK=1
$env:MSSdk=1

python setup.py bdist_wheel

Make sure to use the Visual Studio toolset that matches the Python version that you’re targetting: https://wiki.python.org/moin/WindowsCompilers.

By default, we’re statically linking the VC runtime. To enable dynamic linking, set $env:PYMI_VCRUNTIME_DYNAMIC_LINKING="y".

Debug builds

The easiest way to do a debug build is to set the following in setup.cfg:

[build]
debug = 1

This will be honored regardless of the build type (e.g. stdist, wheel, etc).

To enable distutils debug logging, you may set the following:

$env:DISTUTILS_DEBUG = 1

Before doing a debug build, you may wish to clean the build dir.

Using the Visual Studio Solution

Open the provided PyMI.sln solution in Visual Studio 2015 [4], choose your target Python version / platform and build. Wheel packages are automatically generated in the dist folder for release builds.

Note: the target Python version must be present. The Python path can be customized by setting the corresponding PythonDir* user macro, e.g. PythonDir_34_x64. The wheel and GitPython packages are required during the build process:

pip install wheel
pip install GitPython

As an alternative, you can use the MSBuild CLI tool:

$env:MSBuildEmitSolution="TRUE"
MSBuild.exe .\PyMI.sln /p:Configuration="Release (Python 3.7)"

References

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

PyMI-1.0.7.tar.gz (65.2 kB view details)

Uploaded Source

Built Distributions

PyMI-1.0.7-cp312-cp312-win_amd64.whl (305.0 kB view details)

Uploaded CPython 3.12 Windows x86-64

PyMI-1.0.7-cp311-cp311-win_amd64.whl (304.9 kB view details)

Uploaded CPython 3.11 Windows x86-64

PyMI-1.0.7-cp310-cp310-win_amd64.whl (304.9 kB view details)

Uploaded CPython 3.10 Windows x86-64

File details

Details for the file PyMI-1.0.7.tar.gz.

File metadata

  • Download URL: PyMI-1.0.7.tar.gz
  • Upload date:
  • Size: 65.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.11.1

File hashes

Hashes for PyMI-1.0.7.tar.gz
Algorithm Hash digest
SHA256 d2cc332cfc7eb51405f16fbcb19ae98ce1ec6116b9c406d479f8c1ac8d679460
MD5 3b74709f1993496198ee55ec0b5dcb01
BLAKE2b-256 83ce52d977397828f93f2f6c74500ca2e6a8f2a1ca566acd85f874f0e1b8bebc

See more details on using hashes here.

File details

Details for the file PyMI-1.0.7-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: PyMI-1.0.7-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 305.0 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.11.1

File hashes

Hashes for PyMI-1.0.7-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 9e73eb93cf449c6edcf656aad53df36d8bf593c126131544447b5962905ce3b8
MD5 fb53817b6dd0f81e6774731aa824c26b
BLAKE2b-256 325eefd8cf6059024f50666e0babff5f8afff91683ae408cc6e83e193a203d35

See more details on using hashes here.

File details

Details for the file PyMI-1.0.7-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: PyMI-1.0.7-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 304.9 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.11.1

File hashes

Hashes for PyMI-1.0.7-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 8e1a99f0668a05a3c8b47a4e818a95792634ff82662fabbdcf9b4c76bb8a605d
MD5 d8c68bb4a5319fcafc4579b2c1a38ce1
BLAKE2b-256 0a6f4d4370e8296e64fc350c70410201ae1fde7dc795c2aa917af64cd147398c

See more details on using hashes here.

File details

Details for the file PyMI-1.0.7-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: PyMI-1.0.7-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 304.9 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.11.1

File hashes

Hashes for PyMI-1.0.7-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 a2bbb76af477cf9fe5cc69b4d1e000df917bf57df5bcea2a503ad3bfe0decf14
MD5 a516866e3c1cab82fd140638bc0d2487
BLAKE2b-256 11e77c4861b41859f1cf0c7cbda780bf1107e0f6762d3a38ef6fc2b7a9cab02f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page