webspherejythonwsadmin

passing argument to jython used in wsadmin.sh dynamically


I need a help in wsadmin.sh scripting along with jython. I am creating a script to provide console access to the users via wsadmin.sh I am able to make it work if I hardcode the user name details inside the consoleacces.py file but My requirement is to give the user name details at run-time so that i can use the script for multiple times for different user.

Working :

wsadmin.sh -lang jython  -f /tmp/consoleaccess.py

content of consoleaccess.py

AdminTask.mapUsersToAdminRole('[-accessids [user:defaultWIMFileBasedRealm/employeenumber=123,ou=people,ou=country,o=office] -roleName administrator -userids user1]')
AdminConfig.save()
agBean=AdminControl.queryNames('type=AuthorizationGroupManager,process=dmgr,*');null=AdminControl.invoke(agBean, 'refreshAll')

Not working

wsadmin.sh -lang jython -f /tmp/consoleaccess.py 123 user1 administrator

content of consoleaccess.py

 import sys
    AdminTask.mapUsersToAdminRole('[-accessids [user:defaultWIMFileBasedRealm/employeenumber=sys.argv[1],ou=people,ou=americas,o=SIAM_ED] -roleName sys.argv[3] -userids sys.argv[2]]')
    AdminConfig.save()
    agBean=AdminControl.queryNames('type=AuthorizationGroupManager,process=dmgr,*');null=AdminControl.invoke(agBean, 'refreshAll')

Error :

WASX7209I: Connected to process "dmgr" on node host1 using SOAP connector;  The type of process is: DeploymentManager
WASX7303I: The following options are passed to the scripting environment and are available as arguments that are stored in the argv variable: "[123, user1, administrator]"
WASX7017E: Exception received while running file "/tmp/consoleaccess.py"; exception information: com.ibm.ws.scripting.ScriptingException: WASX8009E: Invalid parameter: [-accessids [user:defaultWIMFileBasedRealm/employeenumber=sys.argv[1],ou=people,ou=americas,o=SIAM_ED] -roleName sys.argv[3] -userids sys.argv[2]]

Solution

  • Update the script as below.

    Note : In wsadmin Jython, the name of the program, or script, is not part of sys.argv. So your first argument is sys.argv[0] and not sys.argv[1]

    import sys
    
    userEmpNo=sys.argv[0]
    userName=sys.argv[1]
    userRole=sys.argv[2]
    
    AdminTask.mapUsersToAdminRole('[-accessids [user:defaultWIMFileBasedRealm/employeenumber=' +userEmpNo+ ',ou=people,ou=americas,o=SIAM_ED] -roleName ' +userRole+ ' -userids ' +userName+ ']')
    AdminConfig.save()
    agBean=AdminControl.queryNames('type=AuthorizationGroupManager,process=dmgr,*');null=AdminControl.invoke(agBean, 'refreshAll')
    

    and run the script as

    wsadmin.sh -lang jython -f /tmp/consoleaccess.py 123 user1 administrator