Home » Querying WMI using Python

Querying WMI using Python

by maxguy71

In this article we look at python and wmi, first what exactly is wmi.

Windows Management Instrumentation (WMI) consists of a set of extensions to the Windows Driver Model that provides an operating system interface through which instrumented components provide information and notification. WMI is Microsoft’s implementation of the Web-Based Enterprise Management (WBEM) and Common Information Model (CIM) standards from the Distributed Management Task Force (DMTF).

WMI allows scripting languages (such as VBScript or Windows PowerShell) to manage Microsoft Windows personal computers and servers, both locally and remotely. WMI comes preinstalled in Windows 2000 and in newer Microsoft OSes. It is available as a download for Windows NT and[1] Windows 95 to Windows 98.[2]

Microsoft also provides a command-line interface to WMI called Windows Management Instrumentation Command-line (WMIC)

Installation

pip install WMI

Code

Lets look at some examples

import wmi
myWMI = wmi.WMI ()

for myTime in myWMI.Win32_LocalTime ():
    print (myTime.Hour,myTime.Minute,myTime.Second)
    print ("%s:%s:%s" % (myTime.Hour, myTime.Minute, myTime.Second))
import wmi
myWMI = wmi.WMI ()

for processer in myWMI.Win32_Processor ():
    print ("Current Clock Speed : %s" % (processer.CurrentClockSpeed))
    print ("Name : %s" % (processer.Name))
    print ("Cores : %s" % (processer.NumberOfCores))
import wmi

ports = wmi.WMI ()

for serialPort in ports.Win32_SerialPort ():
    print ("Name : %s" % (serialPort.Name))

This example does not use the pypi package but it does still required the pywin32 extension

#! python

# To use WMI in Python, install the Python for Windows extensions:
# http://sourceforge.net/projects/pywin32/files/pywin32/

import sys
import win32com.client

try:
	computer = sys.argv[1]
except IndexError:
	computer = "."

wmiservice = win32com.client.Dispatch( "WbemScripting.SWbemLocator" )
swbemservices = wmiservice.ConnectServer( computer, "root/CIMV2" )
instances = swbemservices.ExecQuery( "SELECT * FROM Win32_Processor" )

if instances.Count == 1:
	print( "1 instance:" )
else:
	print( str( instances.Count ) + " instances:" )
print( )

for item in instances:
	if item.AddressWidth == None:
		print( "AddressWidth                            : " )
	else:
		print( "AddressWidth                            : " + str( item.AddressWidth ) )
	if item.Architecture == None:
		print( "Architecture                            : " )
	else:
		print( "Architecture                            : " + str( item.Architecture ) )
	if item.AssetTag == None:
		print( "AssetTag                                : " )
	else:
		print( "AssetTag                                : " + str( item.AssetTag ) )
	if item.Availability == None:
		print( "Availability                            : " )
	else:
		print( "Availability                            : " + str( item.Availability ) )
	if item.Caption == None:
		print( "Caption                                 : " )
	else:
		print( "Caption                                 : " + str( item.Caption ) )
	if item.Characteristics == None:
		print( "Characteristics                         : " )
	else:
		print( "Characteristics                         : " + str( item.Characteristics ) )
	if item.ConfigManagerErrorCode == None:
		print( "ConfigManagerErrorCode                  : " )
	else:
		print( "ConfigManagerErrorCode                  : " + str( item.ConfigManagerErrorCode ) )
	if item.ConfigManagerUserConfig == None:
		print( "ConfigManagerUserConfig                 : " )
	else:
		print( "ConfigManagerUserConfig                 : " + str( item.ConfigManagerUserConfig ) )
	if item.CpuStatus == None:
		print( "CpuStatus                               : " )
	else:
		print( "CpuStatus                               : " + str( item.CpuStatus ) )
	if item.CreationClassName == None:
		print( "CreationClassName                       : " )
	else:
		print( "CreationClassName                       : " + str( item.CreationClassName ) )
	if item.CurrentClockSpeed == None:
		print( "CurrentClockSpeed                       : " )
	else:
		print( "CurrentClockSpeed                       : " + str( item.CurrentClockSpeed ) )
	if item.CurrentVoltage == None:
		print( "CurrentVoltage                          : " )
	else:
		print( "CurrentVoltage                          : " + str( item.CurrentVoltage ) )
	if item.DataWidth == None:
		print( "DataWidth                               : " )
	else:
		print( "DataWidth                               : " + str( item.DataWidth ) )
	if item.Description == None:
		print( "Description                             : " )
	else:
		print( "Description                             : " + str( item.Description ) )
	if item.DeviceID == None:
		print( "DeviceID                                : " )
	else:
		print( "DeviceID                                : " + str( item.DeviceID ) )
	if item.ErrorCleared == None:
		print( "ErrorCleared                            : " )
	else:
		print( "ErrorCleared                            : " + str( item.ErrorCleared ) )
	if item.ErrorDescription == None:
		print( "ErrorDescription                        : " )
	else:
		print( "ErrorDescription                        : " + str( item.ErrorDescription ) )
	if item.ExtClock == None:
		print( "ExtClock                                : " )
	else:
		print( "ExtClock                                : " + str( item.ExtClock ) )
	if item.Family == None:
		print( "Family                                  : " )
	else:
		print( "Family                                  : " + str( item.Family ) )
	if item.InstallDate == None:
		print( "InstallDate                             : " )
	else:
		print( "InstallDate                             : " + str( item.InstallDate ) )
	if item.L2CacheSize == None:
		print( "L2CacheSize                             : " )
	else:
		print( "L2CacheSize                             : " + str( item.L2CacheSize ) )
	if item.L2CacheSpeed == None:
		print( "L2CacheSpeed                            : " )
	else:
		print( "L2CacheSpeed                            : " + str( item.L2CacheSpeed ) )
	if item.L3CacheSize == None:
		print( "L3CacheSize                             : " )
	else:
		print( "L3CacheSize                             : " + str( item.L3CacheSize ) )
	if item.L3CacheSpeed == None:
		print( "L3CacheSpeed                            : " )
	else:
		print( "L3CacheSpeed                            : " + str( item.L3CacheSpeed ) )
	if item.LastErrorCode == None:
		print( "LastErrorCode                           : " )
	else:
		print( "LastErrorCode                           : " + str( item.LastErrorCode ) )
	if item.Level == None:
		print( "Level                                   : " )
	else:
		print( "Level                                   : " + str( item.Level ) )
	if item.LoadPercentage == None:
		print( "LoadPercentage                          : " )
	else:
		print( "LoadPercentage                          : " + str( item.LoadPercentage ) )
	if item.Manufacturer == None:
		print( "Manufacturer                            : " )
	else:
		print( "Manufacturer                            : " + str( item.Manufacturer ) )
	if item.MaxClockSpeed == None:
		print( "MaxClockSpeed                           : " )
	else:
		print( "MaxClockSpeed                           : " + str( item.MaxClockSpeed ) )
	if item.Name == None:
		print( "Name                                    : " )
	else:
		print( "Name                                    : " + str( item.Name ) )
	if item.NumberOfCores == None:
		print( "NumberOfCores                           : " )
	else:
		print( "NumberOfCores                           : " + str( item.NumberOfCores ) )
	if item.NumberOfEnabledCore == None:
		print( "NumberOfEnabledCore                     : " )
	else:
		print( "NumberOfEnabledCore                     : " + str( item.NumberOfEnabledCore ) )
	if item.NumberOfLogicalProcessors == None:
		print( "NumberOfLogicalProcessors               : " )
	else:
		print( "NumberOfLogicalProcessors               : " + str( item.NumberOfLogicalProcessors ) )
	if item.OtherFamilyDescription == None:
		print( "OtherFamilyDescription                  : " )
	else:
		print( "OtherFamilyDescription                  : " + str( item.OtherFamilyDescription ) )
	if item.PartNumber == None:
		print( "PartNumber                              : " )
	else:
		print( "PartNumber                              : " + str( item.PartNumber ) )
	if item.PNPDeviceID == None:
		print( "PNPDeviceID                             : " )
	else:
		print( "PNPDeviceID                             : " + str( item.PNPDeviceID ) )
	if item.PowerManagementCapabilities == None:
		print( "PowerManagementCapabilities             : " )
	else:
		print( "PowerManagementCapabilities             : " + str( item.PowerManagementCapabilities ) )
	if item.PowerManagementSupported == None:
		print( "PowerManagementSupported                : " )
	else:
		print( "PowerManagementSupported                : " + str( item.PowerManagementSupported ) )
	if item.ProcessorId == None:
		print( "ProcessorId                             : " )
	else:
		print( "ProcessorId                             : " + str( item.ProcessorId ) )
	if item.ProcessorType == None:
		print( "ProcessorType                           : " )
	else:
		print( "ProcessorType                           : " + str( item.ProcessorType ) )
	if item.Revision == None:
		print( "Revision                                : " )
	else:
		print( "Revision                                : " + str( item.Revision ) )
	if item.Role == None:
		print( "Role                                    : " )
	else:
		print( "Role                                    : " + str( item.Role ) )
	if item.SecondLevelAddressTranslationExtensions == None:
		print( "SecondLevelAddressTranslationExtensions : " )
	else:
		print( "SecondLevelAddressTranslationExtensions : " + str( item.SecondLevelAddressTranslationExtensions ) )
	if item.SerialNumber == None:
		print( "SerialNumber                            : " )
	else:
		print( "SerialNumber                            : " + str( item.SerialNumber ) )
	if item.SocketDesignation == None:
		print( "SocketDesignation                       : " )
	else:
		print( "SocketDesignation                       : " + str( item.SocketDesignation ) )
	if item.Status == None:
		print( "Status                                  : " )
	else:
		print( "Status                                  : " + str( item.Status ) )
	if item.StatusInfo == None:
		print( "StatusInfo                              : " )
	else:
		print( "StatusInfo                              : " + str( item.StatusInfo ) )
	if item.Stepping == None:
		print( "Stepping                                : " )
	else:
		print( "Stepping                                : " + str( item.Stepping ) )
	if item.SystemCreationClassName == None:
		print( "SystemCreationClassName                 : " )
	else:
		print( "SystemCreationClassName                 : " + str( item.SystemCreationClassName ) )
	if item.SystemName == None:
		print( "SystemName                              : " )
	else:
		print( "SystemName                              : " + str( item.SystemName ) )
	if item.ThreadCount == None:
		print( "ThreadCount                             : " )
	else:
		print( "ThreadCount                             : " + str( item.ThreadCount ) )
	if item.UniqueId == None:
		print( "UniqueId                                : " )
	else:
		print( "UniqueId                                : " + str( item.UniqueId ) )
	if item.UpgradeMethod == None:
		print( "UpgradeMethod                           : " )
	else:
		print( "UpgradeMethod                           : " + str( item.UpgradeMethod ) )
	if item.Version == None:
		print( "Version                                 : " )
	else:
		print( "Version                                 : " + str( item.Version ) )
	if item.VirtualizationFirmwareEnabled == None:
		print( "VirtualizationFirmwareEnabled           : " )
	else:
		print( "VirtualizationFirmwareEnabled           : " + str( item.VirtualizationFirmwareEnabled ) )
	if item.VMMonitorModeExtensions == None:
		print( "VMMonitorModeExtensions                 : " )
	else:
		print( "VMMonitorModeExtensions                 : " + str( item.VMMonitorModeExtensions ) )
	if item.VoltageCaps == None:
		print( "VoltageCaps                             : " )
	else:
		print( "VoltageCaps                             : " + str( item.VoltageCaps ) )
print( )

Links

http://pypi.python.org/pypi/WMI/

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More