WMI with IronPython

From IronPython Cookbook

Revision as of 15:44, 26 October 2007 by MichaelFoord (Talk | contribs)

An example from Vasta Sutra, of using the managed interface to WMI on the local system.

These examples use objects from the System.Management assembly:

import clr
clr.AddReference("System.Management")
from System.Management import (
    ManagementObject,
    ManagementObjectSearcher
)

mo = ManagementObject("Win32_Processor.DeviceID='CPU0'")
for p in mo.Properties: 
    print p.Name, '=', p.Value

query = ManagementObjectSearcher("Select * from Win32_Processor")
mo = query.Get().GetEnumerator().next()

mo = ManagementObject("Win32_PerfFormattedData_PerfOS_Processor.Name='_total'")
for p in mo.Properties: 
    print p.Name, '=', p.Value


This next example reports CPU utilisation:

import clr
clr.AddReference("System.Management")
from System.Management  import ManagementObject
from System.Threading import Thread

while True:
    mo = ManagementObject("Win32_PerfFormattedData_PerfOS_Processor.Name='_total'")
    print mo["PercentProcessorTime"]
    Thread.Sleep(5000)


Back to Contents.

TOOLBOX
LANGUAGES