WMI with IronPython

From IronPython Cookbook

(Difference between revisions)
m
 
(14 intermediate revisions not shown)
Line 1: Line 1:
-
An example from [http://vastusutra.blogspot.com/2007/09/wmi-on-local-system.html Vasta Sutra], of using the managed interface to WMI on the local system.
+
Examples from [http://vastusutra.blogspot.com/ Vasta Sutra], of using the managed interface to WMI on the local system.
These examples use objects from the [http://msdn2.microsoft.com/en-us/library/system.management.aspx System.Management] assembly:
These examples use objects from the [http://msdn2.microsoft.com/en-us/library/system.management.aspx System.Management] assembly:
Line 20: Line 20:
for p in mo.Properties:  
for p in mo.Properties:  
     print p.Name, '=', p.Value
     print p.Name, '=', p.Value
 +
</pre>
 +
 +
 +
This next example reports CPU usage:
 +
<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>
 +
 +
This third example is adapted from [http://artyprog.blogspot.com/2007/12/wmi-ironpython_26.html this blog entry]:
 +
<pre>
 +
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")
 +
</pre>
 +
 +
 +
This example is from an [http://lists.ironpython.com/pipermail/users-ironpython.com/2004-August/000086.html email on the mailing list] from '''2004''':
 +
<pre>
 +
import clr
 +
clr.AddReference('System.Management')
 +
from System.Management import (
 +
    ManagementClass, ManagementScope,
 +
    ManagementPath, ManagementObjectSearcher,
 +
    WqlObjectQuery
 +
)
 +
 +
def showAll(root):
 +
    scope = ManagementScope(root)
 +
    path = ManagementPath("__namespace")
 +
    nsClass = ManagementClass(scope, path, None)
 +
    for namespace in nsClass.GetInstances():
 +
        print "-----"
 +
        for nsProp in namespace.Properties:
 +
            print nsProp.Name, ":", nsProp.Value
 +
        print "--"
 +
        scope = ManagementScope("root\\" + str(namespace.GetPropertyValue("Name")))
 +
        searcher = ManagementObjectSearcher(scope, WqlObjectQuery("select * from meta_class"), None)
 +
        for wmiClass in searcher.Get():
 +
            print "Class:", wmiClass.GetPropertyValue("__CLASS")
 +
 +
showAll('root')
 +
</pre>
 +
 +
 +
This fourth example is converted from C# from the [http://msdn2.microsoft.com/en-us/library/system.management.managementeventwatcher.aspx ManagementEventWatcher] page:
 +
<pre>
 +
import clr
 +
clr.AddReference('System.Management')
 +
from System.Management import *
 +
 +
from System import TimeSpan
 +
 +
 +
query = WqlEventQuery("__InstanceCreationEvent", TimeSpan(0, 0, 1), "TargetInstance isa \"Win32_Process\"")
 +
 +
watcher = ManagementEventWatcher()
 +
watcher.Query = query
 +
watcher.Options.Timeout = TimeSpan(0, 0, 5)
 +
 +
e = watcher.WaitForNextEvent()
 +
 +
# Start a new Win32 process (like notepad) before the timeout
 +
 +
instance = e['TargetInstance']
 +
 +
for entry in instance.Properties:
 +
    print entry.Name, entry.Value
</pre>
</pre>
Back to [[Contents]].
Back to [[Contents]].

Current revision as of 06:22, 8 June 2010

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 an email on the mailing list from 2004:

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

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


This fourth example is converted from C# from the ManagementEventWatcher page:

import clr
clr.AddReference('System.Management')
from System.Management import *

from System import TimeSpan


query = WqlEventQuery("__InstanceCreationEvent", TimeSpan(0, 0, 1), "TargetInstance isa \"Win32_Process\"")

watcher = ManagementEventWatcher()
watcher.Query = query
watcher.Options.Timeout = TimeSpan(0, 0, 5)

e = watcher.WaitForNextEvent()

# Start a new Win32 process (like notepad) before the timeout

instance = e['TargetInstance']

for entry in instance.Properties:
    print entry.Name, entry.Value


Back to Contents.

TOOLBOX
LANGUAGES