WMI with IronPython

From IronPython Cookbook

(Difference between revisions)
Line 50: Line 50:
      
      
print requestWMI("Select * from Win32_BIOS", "Caption")
print requestWMI("Select * from Win32_BIOS", "Caption")
 +
</pre>
 +
 +
 +
This example is from a 2004 [http://lists.ironpython.com/pipermail/users-ironpython.com/2004-August/000086.html email on the mailing list]:
 +
<pre>
 +
import clr
 +
clr.AddReference('System.Management')
 +
from System.Management import (
 +
    ManagementClass, ManagementScope,
 +
    ManagementPath, ManagementObjectSearcher,
 +
    WqlObjectQuery
 +
)
 +
 +
def showAll(root):
 +
    nsClass = ManagementClass(ManagementScope(root), ManagementPath("__namespace"), None)
 +
    for namespace in nsClass.GetInstances():
 +
        print "-----"
 +
        for nsProp in namespace.Properties:
 +
            print str(nsProp.Name), ":", str(nsProp.Value)
 +
    print "--"
 +
    searcher = ManagementObjectSearcher(ManagementScope("root\\" + str(namespace.GetPropertyValue("Name"))), WqlObjectQuery("select * from meta_class"), None)
 +
    for wmiClass in searcher.Get():
 +
        print "Class:", wmiClass.GetPropertyValue("__CLASS")
 +
 +
showAll('root')
</pre>
</pre>
Back to [[Contents]].
Back to [[Contents]].

Revision as of 17:59, 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")


This example is from a 2004 email on the mailing list:

import clr
clr.AddReference('System.Management')
from System.Management import (
    ManagementClass, ManagementScope,
    ManagementPath, ManagementObjectSearcher,
    WqlObjectQuery
)

def showAll(root):
    nsClass = ManagementClass(ManagementScope(root), ManagementPath("__namespace"), None)
    for namespace in nsClass.GetInstances():
        print "-----"
        for nsProp in namespace.Properties:
            print str(nsProp.Name), ":", str(nsProp.Value)
    print "--"
    searcher = ManagementObjectSearcher(ManagementScope("root\\" + str(namespace.GetPropertyValue("Name"))), WqlObjectQuery("select * from meta_class"), None)
    for wmiClass in searcher.Get():
        print "Class:", wmiClass.GetPropertyValue("__CLASS")
 
showAll('root')


Back to Contents.

TOOLBOX
LANGUAGES