Alle brugere i Security group under OU i AD
Hej,Jeg har lavet følgende kode, som fungerer, men jeg skal have lavet en tilføjelse, som jeg har store problemmer med:
namespace BatchJobs
{
public class Program
{
static void Main(string[] args)
{
ADManager.GetListOfAdUsersByGroup("ISS");
Console.ReadLine();
}
public class ADManager
{
public static DirectoryEntry createDirectoryEntry()
{
// create and return new LDAP connection with desired settings
DirectoryEntry ldapConnection = new DirectoryEntry("domain","user","password");
ldapConnection.Path = "LDAP://OU=globalou,DC=localdomain,DC=test,DC=dk";
ldapConnection.AuthenticationType = AuthenticationTypes.Secure;
return ldapConnection;
}
public static void GetListOfAdUsersByGroup(string groupName)
{
DirectoryEntry directoryEntry = createDirectoryEntry();
DirectorySearcher search = new DirectorySearcher(directoryEntry);
string query = "(&(objectCategory=person)(objectClass=user)(memberOf=*))";
search.Filter = query;
search.PropertiesToLoad.Add("memberOf");
search.PropertiesToLoad.Add("name");
search.PropertiesToLoad.Add("givenname");
search.PropertiesToLoad.Add("samaccountname");
search.PropertiesToLoad.Add("sn");
search.PropertiesToLoad.Add("useraccountcontrol");
System.DirectoryServices.SearchResultCollection mySearchResultColl = search.FindAll();
Console.WriteLine("Members of the {0} Group", groupName);
foreach (SearchResult result in mySearchResultColl)
{
string sam = "";
string fname = "";
string lname = "";
string active = "";
string memberOf = "";
foreach (string prop in result.Properties["memberOf"])
{
if (prop.Contains(groupName))
{
memberOf = result.Properties["memberOf"][0].ToString();
sam = result.Properties["samaccountname"][0].ToString();
//fname = result.Properties["givenname"][0].ToString();
//lname = result.Properties["sn"][0].ToString();
active = result.Properties["useraccountcontrol"][0].ToString();
Console.WriteLine(" " + result.Properties["name"][0].ToString());
}
}
}
}
}
}
}
Under globalou er der en security-group, og jeg vil gerne have navnet på alle de brugere, som er medlem af denne gruppe.
Kan du hjælpe mig?