Skip to main content

Python WMI

Windows Management Interface connector using asyncio for the Python language.

Supports:

  • NTLM Authentication
  • WMI Query (IWbemServices_ExecQuery)
  • Parsing of basic WMI Objects (int/float/str/datetime/array/references)

Todo:

  • Kerberos Authentication
  • Async WMI Query (IWbemServices_ExecQueryAsync)
  • Other DCOM/RPC/WMI calls?
  • Support for WMI Methods?
  • Improve documentation

Usage

The example below covers most of what is suppored by this library:

import asyncio
import logging
import time
from aiowmi.connection import Connection
from aiowmi.query import Query
from aiowmi.exceptions import WbemStopIteration


async def main():

    host = '10.0.0.1'  # ip address or hostname or fqdn
    username = 'username'
    password = 'password'
    domain = ''  # optional domain name

    # Query has a default namespace 'root/cimv2'
    queries = (
        Query('SELECT * FROM Win32_OperatingSystem', namespace='root/cimv2'),
        Query('SELECT * FROM Win32_NetworkAdapter'),
        Query('SELECT * FROM Win32_LoggedOnUser'),
        Query('SELECT * FROM Win32_PnpEntity'),
        Query('SELECT Caption, Description, InstallDate, InstallDate2, '
              'InstallLocation, InstallSource, InstallState, Language, '
              'LocalPackage, Name, PackageCache, PackageCode, PackageName, '
              'ProductID, RegCompany, RegOwner, SKUNumber, Transforms, '
              'URLInfoAbout, URLUpdateInfo, Vendor, Version '
              'FROM Win32_Product'),
        Query('SELECT Name, DiskReadsPersec, DiskWritesPersec '
              'FROM Win32_PerfFormattedData_PerfDisk_LogicalDisk'),
    )

    start = time.time()

    conn = Connection(host, username, password, domain=domain)
    service = None
    await conn.connect()
    try:
        service = await conn.negotiate_ntlm()

        for query in queries:
            print(f"""
###############################################################################
# Start Query: {query.query}
###############################################################################
""")
            await query.start(conn, service)

            while True:
                try:
                    res = await query.next()
                except WbemStopIteration:
                    break

                # Function `get_properties(..)` accepts a few keyword arguments:
                #
                # ignore_defaults:
                #        Ignore default values. Set missing values to None
                #        if a value does not exist in the current class.
                #        ignore_defaults will always be True if ignore_missing
                #        is set to True.
                # ignore_missing:
                #       If set to True, values missing in the current class
                #       will not be part of the result.
                # load_qualifiers:
                #       Load the qualifiers of the properties. If False, the
                #       property qualifier_set will have the offset in the
                #       heap where the qualifiers are stored.
                #
                props = res.get_properties()

                for name, prop in props.items():
                    print(name, '\n\t', prop.value)

                    if prop.is_reference():
                        # References can easy be queried using the
                        # get_reference(..) function. The function accepts a
                        # keyword argument `filter_props=[..]` with an optional
                        # list of properties to query. If omitted, the function
                        # returns all (*) properties.
                        res = await prop.get_reference(service)
                        ref_props = res.get_properties(ignore_missing=True)
                        for name, prop in ref_props.items():
                            print('\t\t', name, '\n\t\t\t', prop.value)

                print(f"""
----------------------------------- End Item ----------------------------------
""")
    finally:
        if service:
            service.close()
        conn.close()
        end = time.time()
        print('done in ', end-start)

if __name__ == '__main__':
    logger = logging.getLogger()
    logger.setLevel(logging.DEBUG)

    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)

    formatter = logging.Formatter(
            fmt='[%(levelname)1.1s %(asctime)s %(module)s:%(lineno)d] ' +
                '%(message)s',
            datefmt='%y%m%d %H:%M:%S',
            style='%')

    ch.setFormatter(formatter)

    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())

Download files

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

Source Distribution

aiowmi-0.1.1.tar.gz (71.9 kB view details)

Uploaded Source

File details

Details for the file aiowmi-0.1.1.tar.gz.

File metadata

  • Download URL: aiowmi-0.1.1.tar.gz
  • Upload date:
  • Size: 71.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.12

File hashes

Hashes for aiowmi-0.1.1.tar.gz
Algorithm Hash digest
SHA256 704acda3877f2b891e7acd111c72d8bf55f6c0c79be66106e1e79ec432a99c04
MD5 b5205d1ae483d0fcddaf656feb1975b5
BLAKE2b-256 2b5fa2e1cdf084be9d6ab60e1c3fe6f0400468ea1cf6eabeadff7a3e4bb9acec

See more details on using hashes here.

Release history Release notifications | RSS feed

1.1.3

2 files

1.1.2

2 files

1.1.1

2 files

1.1.0

2 files

1.0.10

2 files

1.0.9

2 files

1.0.8

2 files

1.0.7

2 files

1.0.6

2 files

1.0.5

2 files

1.0.4

2 files

1.0.3

2 files

1.0.2

2 files

1.0.1

2 files

1.0.0

2 files

0.2.3

1 file

0.2.2

1 file

0.2.1

1 file

0.2.0

1 file

0.1.21

1 file

0.1.20

1 file

0.1.19

1 file

0.1.18

1 file

0.1.17

1 file

0.1.16

1 file

0.1.15

1 file

0.1.14

1 file

0.1.13

1 file

0.1.12

1 file

0.1.11

1 file

0.1.10

1 file

0.1.9

1 file

0.1.8

1 file

0.1.7

1 file

0.1.6

1 file

0.1.5

1 file

0.1.4

1 file

0.1.3

1 file

0.1.2

1 file

This release

0.1.1 This release

1 file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page