The EnumerateInstances()
method returns a list of pywbem.CIMInstance
objects for each CIM instance of a particular CIM class (and its subclasses). The CIM instance path is part of the returned instance objects and can be accessed via their path
attribute.
The following code enumerates the instances of the specified CIM class and prints their instance path in WBEM URI format (see pywbem.CIMInstanceName.__str__()
), and the instance itself in MOF format (see pywbem.CIMInstance.tomof()
).
In [ ]:
from __future__ import print_function
import pywbem
username = 'user'
password = 'password'
classname = 'CIM_ComputerSystem'
namespace = 'root/cimv2'
server = 'http://localhost'
conn = pywbem.WBEMConnection(server, (username, password),
default_namespace=namespace,
no_verification=True)
try:
insts = conn.EnumerateInstances(classname)
except pywbem.Error as exc:
print('Operation failed: %s' % exc)
else:
print('Retrieved %s instances' % (len(insts)))
for inst in insts:
print('path=%s' % inst.path)
print(inst.tomof())
In this example, the connection has a default namespace set. This allows us to omit the namespace from the subsequent EnumerateInstances()
method.
This example also shows exception handling with PyWBEM: PyWBEM wraps any exceptions that are considered runtime errors, and raises them as subclasses of pywbem.Error
. Any other exceptions are considered programming errors. Therefore, the code above only needs to catch pywbem.Error
.
Note that the creation of the pywbem.WBEMConnection
object in the code above does not need to be protected by exception handling; its initialization code does not raise any PyWBEM runtime errors.