Friday, July 24, 2020

Get the users from OU

                 // fetch the service accounts from desired  OU 
public void OUSearch(String domain) throws NamingException {

System.out.println("Entering into OUSearch method : "); 
                 String searchFilter = "(&(objectClass=person))";   

SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);

NamingEnumeration<SearchResult> answer = adConnection.search(domain,
searchFilter, searchControls);
                 int i = 0; 

                 while (answer.hasMoreElements()) {                
SearchResult searchResult = (SearchResult) answer.next(); 
                 i++;
System.out.println("Name : " + searchResult.getName()); 
                 Attributes attrs = searchResult.getAttributes(); 
                 String samAccountName = getAttributeValue(attrs.get("sAMAccountName").toString());
System.out.println("SamAccountName is : " + samAccountName); }
System.out.println("Total results: " + i); 
                }

private String getAttributeValue(String input) { 

                 String[] tempArray = input.split(":"); 
                 String outPut = tempArray[1].trim(); 
                 return outPut;
                 }

                NOTE : String domain = "OU=Users,DC=XYZ,DC=com";

No comments:

Post a Comment

Fetch Members from Workgroup

  import java.util.ArrayList;   import java.util.Iterator;   import java.util.List;   import sailpoint.api.ObjectUtil;   import sailpoint.ob...