#!/usr/bin/env python
# coding: utf-8
# # EnumerateInstances
# <--- Back
# The [`EnumerateInstances()`](https://pywbem.readthedocs.io/en/latest/client.html#pywbem.WBEMConnection.EnumerateInstances) method returns a list of [`pywbem.CIMInstance`](https://pywbem.readthedocs.io/en/latest/client.html#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__()`](https://pywbem.readthedocs.io/en/latest/client.html#pywbem.CIMInstanceName.__str__)), and the instance itself in MOF format (see [`pywbem.CIMInstance.tomof()`](https://pywbem.readthedocs.io/en/latest/client.html#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()`](https://pywbem.readthedocs.io/en/latest/client.html#pywbem.WBEMConnection.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`](https://pywbem.readthedocs.io/en/latest/client.html#pywbem.Error). Any other exceptions are considered programming errors. Therefore, the code above only needs to catch [`pywbem.Error`](https://pywbem.readthedocs.io/en/latest/client.html#pywbem.Error).
#
# Note that the creation of the [`pywbem.WBEMConnection`](https://pywbem.readthedocs.io/en/latest/client.html#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.
# <--- Back