{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# InvokeMethod"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"#\" onclick=\"history.back()\">&lt;--- Back</a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "CIM method invocations are quite easily done. The [`InvokeMethod()`](https://pywbem.readthedocs.io/en/latest/client.html#pywbem.WBEMConnection.InvokeMethod) method is used to invoke a CIM method on a CIM instance, or on a CIM class (only for static CIM methods).\n",
    "\n",
    "For invoking a CIM method on a CIM instance, `InvokeMethod()` takes a [`pywbem.CIMInstanceName`](https://pywbem.readthedocs.io/en/latest/client.html#pywbem.CIMInstanceName) object referencing the CIM instance, as input. The input parameters for the CIM method are specified as keyword parameters to `InvokeMethod()`.\n",
    "\n",
    "`InvokeMethod()` returns a tuple consisting of the return value of the CIM method, and a dictionary with the output parameters of the CIM method."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from __future__ import print_function\n",
    "import sys\n",
    "import pywbem\n",
    "\n",
    "username = 'user'\n",
    "password = 'password'\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",
    "\n",
    "os_class_path = pywbem.CIMClassName('CIM_OperatingSystem', namespace=namespace)\n",
    "\n",
    "print('Invoking CIM_OperatingSystem: MyStaticMethod')\n",
    "try:\n",
    "    result, outparams = conn.InvokeMethod('MyStaticMethod', os_class_path)\n",
    "except pywbem.Error as exc:\n",
    "    if isinstance(exc, pywbem.CIMError) and exc.status_code == pywbem.CIM_ERR_NOT_SUPPORTED:\n",
    "        print('WBEM server does not support method invocation')\n",
    "    elif isinstance(exc, pywbem.CIMError) and exc.status_code == CIM_ERR_METHOD_NOT_FOUND:\n",
    "        print('Method does not exist on class CIM_OperatingSystem: MyStaticMethod')\n",
    "    else:\n",
    "        print('InvokeMethod(MyStaticMethod) failed: %s: %s' % (exc.__class__.__name__, exc))\n",
    "        sys.exit(1)\n",
    "\n",
    "try:\n",
    "    os_inst_paths = conn.EnumerateInstanceNames('CIM_OperatingSystem')\n",
    "except pywbem.Error as exc:\n",
    "    print('EnumerateInstanceNames failed: %s: %s' % (exc.__class__.__name__, exc))\n",
    "    sys.exit(1)\n",
    "\n",
    "os_inst_path = os_inst_paths[0]\n",
    "\n",
    "print('Invoking CIM_OperatingSystem: MyMethod')\n",
    "try:\n",
    "    result, outparams = conn.InvokeMethod('MyMethod', os_inst_path)\n",
    "except pywbem.Error as exc:\n",
    "    if isinstance(exc, pywbem.CIMError) and exc.status_code == pywbem.CIM_ERR_NOT_SUPPORTED:\n",
    "        print('WBEM server does not support method invocation')\n",
    "    elif isinstance(exc, pywbem.CIMError) and exc.status_code == CIM_ERR_METHOD_NOT_FOUND:\n",
    "        print('Method does not exist on class CIM_OperatingSystem: MyMethod')\n",
    "    else:\n",
    "        print('InvokeMethod(MyMethod) failed: %s: %s' % (exc.__class__.__name__, exc))\n",
    "        sys.exit(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"#\" onclick=\"history.back()\">&lt;--- Back</a>"
   ]
  }
 ],
 "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
}
