WMI with IronPython

From IronPython Cookbook

(Difference between revisions)
Line 36: Line 36:
</pre>
</pre>
-
This third example is from [http://artyprog.blogspot.com/2007/12/wmi-ironpython_26.html this blog entry]:
+
This third example is adapted from [http://artyprog.blogspot.com/2007/12/wmi-ironpython_26.html this blog entry]:
<pre>
<pre>
-
import System
 
import clr
import clr
clr.AddReference("System.Management")
clr.AddReference("System.Management")
-
import System.Management
+
from System.Management import ManagementObjectSearcher
def requestWMI(squery,sproperties):
def requestWMI(squery,sproperties):
     output = ""
     output = ""
-
     objMgt = System.Management.ManagementObjectSearcher(squery)
+
     objMgt = ManagementObjectSearcher(squery)
     for result in objMgt.Get():
     for result in objMgt.Get():
         output += result.GetPropertyValue(sproperties)
         output += result.GetPropertyValue(sproperties)

Revision as of 17:07, 26 December 2007

Examples 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 usage:

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)

This third example is adapted from this blog entry:

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

def requestWMI(squery,sproperties):
    output = ""
    objMgt = ManagementObjectSearcher(squery)
    for result in objMgt.Get():
        output += result.GetPropertyValue(sproperties)
    return output
    
print requestWMI("Select * from Win32_BIOS", "Caption")


Back to Contents.

TOOLBOX
LANGUAGES