WMI with IronPython

From IronPython Cookbook

(Difference between revisions)
Line 22: Line 22:
</pre>
</pre>
 +
 +
This next example reports CPU utilisation:
 +
<pre>
 +
 +
 +
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)
 +
</pre>
Back to [[Contents]].
Back to [[Contents]].

Revision as of 15:44, 26 October 2007

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