Maximo Integration and Scripting

Expand all | Collapse all

Define a library of automationscripts that can be used in other automationscripts

  • 1.  Define a library of automationscripts that can be used in other automationscripts

    Posted Fri March 28, 2025 01:43 PM

    Why?

    Why did we look for another option to define jython functions that can be used in another jython automationscript?
    We found the other methods complex

    What did we come up with?

    An automationscript with a function that retrieves an automationscript and returns the script source
    After retrieving the script source we 'exec(ute)' the script source so the function becomes available for use

    What do we need?

    (1) An automationscript (LIB_FUNCTION_001) containing a function that we want to use in another automationscript:
    Lets say you want to perform some simple calculations in a function called iIncrease that has one parameter iIncreaseMe
    if iIncreaseMe == 1 you want to add 1
    if iInCreaseMe == 2 you want to add 2
    otherwise you want to add 10

    Writing a jython function for this would look something like:
    def iIncrease(iIncreaseMe):
        print "Function:iIncrease(" + str(iIncreaseMe) + ") - Start"
        if (iIncreaseMe == 1):
            iReturnValue =  iIncreaseMe + 1
        elif (iIncreaseMe == 2):
            iReturnValue =  iIncreaseMe + 2
        else:
            iReturnValue =  iIncreaseMe + 10
        print "Function:iIncrease(" + str(iIncreaseMe) + ") - End - Returning:[" + str(iReturnValue) + "]"
        return iReturnValue

    (2) Another automationscript (USE_LIB_FUNCTION) that retrieves the script source of LIB_FUNCTION_001 and then 'exec(ute)' the script source so the function becomes available for use

    We have therefore defined the following automationscript called USE_LIB_FUNCTION:
    ----- Script START ----------
    from psdi.server import MXServer
    from com.ibm.tivoli.maximo.script import ScriptCache

    ICDServer = MXServer.getMXServer()
    ICDUser = ICDServer.getSystemUserInfo()

    def strGetAutoscriptSource(strScriptName):
        scriptInfo = ScriptCache.getInstance().getScriptInfo(strScriptName)
        return scriptInfo.getScriptSource()

    print "============================================(1)"
    print "Read autoscript LIB_FUNCTION_001"
    strFunction = strGetAutoscriptSource("LIB_FUNCTION_001")
    print "============================================(2)"
    print "strFunction:"
    print strFunction
    print "============================================(3)"
    print "Performing Exec - Read Function into memory"
    exec(strFunction)
    print "============================================(4)"
    print "Use Read Function - by executing iValue = iIncrease(1)"
    iValue = iIncrease(1)
    print "iValue:"
    print str(iValue)
    print "============================================(5)"
    print "Use Read Function - by executing iValue = iIncrease(2)"
    iValue = iIncrease(2)
    print "iValue:"
    print str(iValue)
    print "============================================(6)"
    print "Use Read Function - by executing iValue = iIncrease(10)"
    iValue = iIncrease(10)
    print "iValue:"
    print str(iValue)
    print "============================================(7)"
    ----- Script END ----------

    Result:

    When we execute the script by pressing (the re-introdued) run script button we see the following output:
    ----- Output START ----------
    ============================================(1)
    Read autoscript LIB_FUNCTION_001
    ============================================(2)
    strFunction:
    def iIncrease(iIncreaseMe):
        print "Function:iIncrease(" + str(iIncreaseMe) + ") - Start"
        if (iIncreaseMe == 1):
            iReturnValue =  iIncreaseMe + 1
        elif (iIncreaseMe == 2):
            iReturnValue =  iIncreaseMe + 2
        else:
            iReturnValue =  iIncreaseMe + 10
        print "Function:iIncrease(" + str(iIncreaseMe) + ") - End - Returning:[" + str(iReturnValue) + "]"
        return iReturnValue
    ============================================(3)
    Performing Exec - Read Function into memory
    ============================================(4)
    Use Read Function - by executing iValue = iIncrease(1)
    Function:iIncrease(1) - Start
    Function:iIncrease(1) - End - Returning:[2]
    iValue:
    2
    ============================================(5)
    Use Read Function - by executing iValue = iIncrease(2)
    Function:iIncrease(2) - Start
    Function:iIncrease(2) - End - Returning:[4]
    iValue:
    4
    ============================================(6)
    Use Read Function - by executing iValue = iIncrease(10)
    Function:iIncrease(10) - Start
    Function:iIncrease(10) - End - Returning:[20]
    iValue:
    20
    ============================================(7)
    ----- Output END ----------

    Let us know what you think...!



    ------------------------------
    Erwin Hebing
    ------------------------------


  • 2.  RE: Define a library of automationscripts that can be used in other automationscripts

    Posted Mon March 31, 2025 09:58 AM

    Hi @Erwin Hebing ,
    I have been using this approach for a long time because I find it more functional than "service.invokeScript()" (which is "the" supported one and generic with respect to the scripting language, so it would also work with Groovy, for example).
    Warning: the service object is not always available as you might expect. I will write a post about this as soon as possible.



    ------------------------------
    Diego Visentin
    ------------------------------