InvokeMethod

CIM method invocations are quite easily done. The InvokeMethod() method is used to invoke a CIM method on a CIM instance, or on a CIM class (only for static CIM methods).

For invoking a CIM method on a CIM instance, InvokeMethod() takes a pywbem.CIMInstanceName object referencing the CIM instance, as input. The input parameters for the CIM method are specified as keyword parameters to InvokeMethod().

InvokeMethod() returns a tuple consisting of the return value of the CIM method, and a dictionary with the output parameters of the CIM method.

In [ ]:
from __future__ import print_function
import sys
import pywbem

username = 'user'
password = 'password'
namespace = 'root/cimv2'
server = 'http://localhost'

conn = pywbem.WBEMConnection(server, (username, password),
                             default_namespace=namespace,
                             no_verification=True)

os_class_path = pywbem.CIMClassName('CIM_OperatingSystem', namespace=namespace)

print('Invoking CIM_OperatingSystem: MyStaticMethod')
try:
    result, outparams = conn.InvokeMethod('MyStaticMethod', os_class_path)
except pywbem.Error as exc:
    if isinstance(exc, pywbem.CIMError) and exc.status_code == pywbem.CIM_ERR_NOT_SUPPORTED:
        print('WBEM server does not support method invocation')
    elif isinstance(exc, pywbem.CIMError) and exc.status_code == CIM_ERR_METHOD_NOT_FOUND:
        print('Method does not exist on class CIM_OperatingSystem: MyStaticMethod')
    else:
        print('InvokeMethod(MyStaticMethod) failed: %s: %s' % (exc.__class__.__name__, exc))
        sys.exit(1)

try:
    os_inst_paths = conn.EnumerateInstanceNames('CIM_OperatingSystem')
except pywbem.Error as exc:
    print('EnumerateInstanceNames failed: %s: %s' % (exc.__class__.__name__, exc))
    sys.exit(1)

os_inst_path = os_inst_paths[0]

print('Invoking CIM_OperatingSystem: MyMethod')
try:
    result, outparams = conn.InvokeMethod('MyMethod', os_inst_path)
except pywbem.Error as exc:
    if isinstance(exc, pywbem.CIMError) and exc.status_code == pywbem.CIM_ERR_NOT_SUPPORTED:
        print('WBEM server does not support method invocation')
    elif isinstance(exc, pywbem.CIMError) and exc.status_code == CIM_ERR_METHOD_NOT_FOUND:
        print('Method does not exist on class CIM_OperatingSystem: MyMethod')
    else:
        print('InvokeMethod(MyMethod) failed: %s: %s' % (exc.__class__.__name__, exc))
        sys.exit(1)