@ For & ever 2009-12-31
Refer to the VMware WebService API documentation to create ESX | esxi users and assign them to user groups.
Version: ESX | esxi 4.0
JavaCodeAs follows:
Import java.net. URL; </P> <p> Import COM. VMware. vim25.hostaccountspec; <br/> Import COM. VMware. vim25.managedobjectreference; <br/> Import COM. VMware. vim25.permission; <br/> Import COM. VMware. vim25.servicecontent; <br/> Import COM. VMware. vim25.vimporttype; <br/> Import COM. VMware. vim25.vimservicelocator; <br/> Import COM. VMware. security. credstore. *; </P> <p>/** <br/> * <p> <br/> * Create an ESX or esxi user (non-vcenter) <br /> * </P> <br/> * @ author forandever <br/> * @ date 2009-12-31 <br/> */<br/> public class createuser {</P> <p>/** <br/> * create a user <br/> * @ Param hostname Host IP address. Example: 202.101.1.121 <br/> * @ Param username connection username. For example, root <br/> * @ Param password indicates the connection password. Example: 123456 <br/> * @ throws exception <br/> */<br/> private void createuser (string hostname, string username, string password) throws exception {<br/> serverconn conn = new serverconn (); <br/> Conn. prepare (hostname, username, password); </P> <p> managedobjectreference hostlocalaccountmanager = <br/> Conn. get_sic (). getaccountmanager (); </P> <p> managedobjectreference hostauthorizationmanager = <br/> Conn. get_sic (). getauthorizationmanager (); </P> <p> string new_username = generateusername (); <br/> string new_password = generatepassword (); </P> <p> hostaccountspec = new hostaccountspec (); <br/> hostaccountspec. setid (new_username); <br/> hostaccountspec. setpassword (new_password); <br/> hostaccountspec. setdescription ("this is a new user created through API"); </P> <p> Conn. get_service (). createuser (hostlocalaccountmanager, <br/> hostaccountspec); </P> <p> system. out. println (new_username + "" + new_password); </P> <p> managedobjectreference rootfolder = <br/> Conn. get_sic (). getrootfolder (); </P> <p> // set the permission group <br/> permission per = new permission (); <br/> Per. setgroup (false); <br/> Per. setprincipal (new_username); <br/> Per. setroleid (-1); <br/> Per. setpropagate (true); <br/> Per. setentity (rootfolder); <br/> Conn. get_service (). setentitypermissions (hostauthorizationmanager, <br/> rootfolder, <br/> New permission [] {per}); </P> <p> // credentialstore csobj = credentialstorefactory. getcredentialstore (); <br/> // csobj. addpassword (getservername (), username, password. tochararray (); <br/> system. out. println ("new user created successfully"); <br/>}</P> <p> Public static void main (string [] ARGs) throws exception {<br/> createuser = new createuser (); <br/> createuser. createuser ("202.101.1.121", "root", "123456"); <br/>}< br/>
@ Forandever 2009-12-31