標籤:java 移動 ms aduser 到指定ou
JAVA移動MSADUser到指定OU
近期做OA系統自動化,所以應用到了一些開發,前面文章我們介紹了使用JAVA建立MS ADUser、修改密碼等,但是建立後,我們還有離職相關的自動化,我們對使用者離職操作為先是禁用使用者,然後移動到指定OU下,然後通過powershell或者vbs指令碼每個月執行一次刪除,而不是對於離職使用者賬戶直接刪除操作,所以我們對於上面的需求,需要做兩步驟,首先是禁用,對于禁用使用者我們一般是修改使用者的useracountcontrol屬性來操作的,我們將會在下一篇文章中介紹AD使用者的屬性。今天我們主要示範使用JAVA程式移動使用者到指定OU 中。
我們環境中oadi這個使用者在IXM Adm OU下,我們需要將移動到IXM Users OU下
650) this.width=650;" title="image" style="margin:0px;background-image:none;" border="0" alt="image" src="http://s3.51cto.com/wyfs02/M00/A6/9B/wKioL1nU21vA0295AAFSEXqX6TI472.png" height="359" />
配置JAVA相關代理
/** * moveuser.java * 5 July 2001 * Sample JNDI application to move an object in the Active Directory. * */import java.util.Hashtable;import javax.naming.*;import javax.naming.ldap.*;import javax.naming.directory.*;public class moveuser { public static void main (String[] args) { Hashtable env = new Hashtable(); String adminName = "CN=Administrator,cn=Users,DC=ixmsoft,DC=com"; String adminPassword = "123"; String oldUserpath = "CN=aodi,OU=IXM Adm,OU=IMXSOFT Users,DC=ixmsoft,DC=com"; String newUserpath = "CN=aodi,OU=IXM Users,OU=IMXSOFT Users,DC=ixmsoft,DC=com"; String ldapURL = "ldap://192.168.5.20:389"; env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory"); //set security credentials, note using simple cleartext authentication env.put(Context.SECURITY_AUTHENTICATION,"simple"); env.put(Context.SECURITY_PRINCIPAL,adminName); env.put(Context.SECURITY_CREDENTIALS,adminPassword); //connect to my domain controller env.put(Context.PROVIDER_URL,ldapURL); try { // Create the initial directory context LdapContext ctx = new InitialLdapContext(env,null); // Move the user ctx.rename(oldUserpath,newUserpath); System.out.println("Moved user"); ctx.close(); } catch (NamingException e) { System.err.println("Problem moving object: " + e); } }}
我們定義新、舊使用者路勁
650) this.width=650;" title="image" style="margin:0px;background-image:none;" border="0" alt="image" src="http://s3.51cto.com/wyfs02/M02/07/E9/wKiom1nU26egGnu3AAH1sJCyjpI475.png" height="378" />
開始執行
650) this.width=650;" title="image" style="margin:0px;background-image:none;" border="0" alt="image" src="http://s3.51cto.com/wyfs02/M00/07/E9/wKiom1nU26mQSe1JAAGuNq3FJQU461.png" height="338" />
執行成功
650) this.width=650;" title="image" style="margin:0px;background-image:none;" border="0" alt="image" src="http://s3.51cto.com/wyfs02/M01/07/E9/wKiom1nU26zhsB1EAAKOrJyzbsU673.png" height="477" />
我們查看AD資訊
650) this.width=650;" title="image" style="margin:0px;background-image:none;" border="0" alt="image" src="http://s3.51cto.com/wyfs02/M00/A6/9B/wKioL1nU22PAERupAAHJ9R-Is8w809.png" height="451" />
我們最後為了方便可以將userpath的CN使用者值定義成變數,方便傳值
/** * moveuser.java * 5 July 2001 * Sample JNDI application to move an object in the Active Directory. * */import java.util.Hashtable;import javax.naming.*;import javax.naming.ldap.*;import javax.naming.directory.*;public class moveuser { public static void main (String[] args) { Hashtable env = new Hashtable(); String ADAccount="aodi"; String adminName = "CN=Administrator,cn=Users,DC=ixmsoft,DC=com"; String adminPassword = "123"; String oldUserpath = "CN="+ADAccount+","+"OU=IXM Adm,OU=IMXSOFT Users,DC=ixmsoft,DC=com"; String newUserpath = "CN="+ADAccount+","+"OU=IXM Users,OU=IMXSOFT Users,DC=ixmsoft,DC=com"; String ldapURL = "ldap://192.168.5.20:389"; env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory"); //set security credentials, note using simple cleartext authentication env.put(Context.SECURITY_AUTHENTICATION,"simple"); env.put(Context.SECURITY_PRINCIPAL,adminName); env.put(Context.SECURITY_CREDENTIALS,adminPassword); //connect to my domain controller env.put(Context.PROVIDER_URL,ldapURL); try { // Create the initial directory context LdapContext ctx = new InitialLdapContext(env,null); // Move the user ctx.rename(oldUserpath,newUserpath); System.out.println("Moved user:"+ADAccount); System.out.println("NewUserpath:"+newUserpath); ctx.close(); } catch (NamingException e) { System.err.println("Problem moving object: " + e); } }}
650) this.width=650;" src="https://s1.51cto.com/wyfs02/M01/07/EC/wKiom1nVlviQey3UAAJWzSHHAiI422.jpg-wh_500x0-wm_3-wmp_4-s_2972007918.jpg" title="1.jpg" alt="wKiom1nVlviQey3UAAJWzSHHAiI422.jpg-wh_50" />
本文出自 “高文龍” 部落格,謝絕轉載!
JAVA移動MS ADUser到指定OU