{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# EnumerateInstances"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<--- Back"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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.\n",
"\n",
"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))."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from __future__ import print_function\n",
"import pywbem\n",
"\n",
"username = 'user'\n",
"password = 'password'\n",
"classname = 'CIM_ComputerSystem'\n",
"namespace = 'root/cimv2'\n",
"server = 'http://localhost'\n",
"\n",
"conn = pywbem.WBEMConnection(server, (username, password),\n",
" default_namespace=namespace,\n",
" no_verification=True)\n",
"try:\n",
" insts = conn.EnumerateInstances(classname)\n",
"except pywbem.Error as exc:\n",
" print('Operation failed: %s' % exc)\n",
"else:\n",
" print('Retrieved %s instances' % (len(insts)))\n",
" for inst in insts:\n",
" print('path=%s' % inst.path)\n",
" print(inst.tomof()) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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.\n",
"\n",
"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).\n",
"\n",
"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."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<--- Back"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}