EDN Admin
Well-known member
Hey all,
Basically what i want to do is be able to run this console app and it goes through every user in active directory and gets every group that user is a member of and spits it out into a log file. The following is what i have so far which is failing to work.
Can someone tell me either where i was going wrong or another method of doing this.
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; namespace Enumerate_AD
{
<span style="color:Blue; class Program
{
<span style="color:Blue; public <span style="color:Blue; static <span style="color:Blue; string domainName = <span style="color:#A31515; "domain";
<span style="color:Blue; static <span style="color:Blue; void Main(<span style="color:Blue; string[] args)
{
PrincipalContext ctx = <span style="color:Blue; new PrincipalContext(ContextType.Domain, domainName);
UserPrincipal user = <span style="color:Blue; new UserPrincipal(ctx);
user.Name = <span style="color:#A31515; "*";
PrincipalSearcher ps = <span style="color:Blue; new PrincipalSearcher();
ps.QueryFilter = user;
PrincipalSearchResult<Principal> result = ps.FindAll();
<span style="color:Blue; foreach (Principal p <span style="color:Blue; in result)
{
<span style="color:Blue; using (UserPrincipal up = (UserPrincipal)p)
{
accounts(up.Name);
}
}
Console.ReadLine();
}
<span style="color:Blue; static <span style="color:Blue; void accounts(<span style="color:Blue; string userName)
{
<span style="color:Blue; if (userName != <span style="color:Blue; null)
{
PrincipalContext ctx = <span style="color:Blue; new PrincipalContext(ContextType.Domain, domainName);
UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, userName);
GroupPrincipal grp = GroupPrincipal.FindByIdentity(ctx, IdentityType.UserPrincipalName, userName);
<span style="color:Green; //PrincipalSearchResult groups = userPrincipal.GetAuthorizationGroups();
PrincipalSearchResult<Principal> groups = userPrincipal.GetAuthorizationGroups();
<span style="color:Blue; if (groups != <span style="color:Blue; null)
{
<span style="color:Blue; foreach (GroupPrincipal l <span style="color:Blue; in groups)
{
Console.WriteLine(userName);
<span style="color:Blue; foreach (GroupPrincipal g <span style="color:Blue; in groups)
{
Console.WriteLine(g.DisplayName);
}
}
}
}
Console.ReadLine();
}
}
}
[/code]
View the full article
Basically what i want to do is be able to run this console app and it goes through every user in active directory and gets every group that user is a member of and spits it out into a log file. The following is what i have so far which is failing to work.
Can someone tell me either where i was going wrong or another method of doing this.
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; namespace Enumerate_AD
{
<span style="color:Blue; class Program
{
<span style="color:Blue; public <span style="color:Blue; static <span style="color:Blue; string domainName = <span style="color:#A31515; "domain";
<span style="color:Blue; static <span style="color:Blue; void Main(<span style="color:Blue; string[] args)
{
PrincipalContext ctx = <span style="color:Blue; new PrincipalContext(ContextType.Domain, domainName);
UserPrincipal user = <span style="color:Blue; new UserPrincipal(ctx);
user.Name = <span style="color:#A31515; "*";
PrincipalSearcher ps = <span style="color:Blue; new PrincipalSearcher();
ps.QueryFilter = user;
PrincipalSearchResult<Principal> result = ps.FindAll();
<span style="color:Blue; foreach (Principal p <span style="color:Blue; in result)
{
<span style="color:Blue; using (UserPrincipal up = (UserPrincipal)p)
{
accounts(up.Name);
}
}
Console.ReadLine();
}
<span style="color:Blue; static <span style="color:Blue; void accounts(<span style="color:Blue; string userName)
{
<span style="color:Blue; if (userName != <span style="color:Blue; null)
{
PrincipalContext ctx = <span style="color:Blue; new PrincipalContext(ContextType.Domain, domainName);
UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, userName);
GroupPrincipal grp = GroupPrincipal.FindByIdentity(ctx, IdentityType.UserPrincipalName, userName);
<span style="color:Green; //PrincipalSearchResult groups = userPrincipal.GetAuthorizationGroups();
PrincipalSearchResult<Principal> groups = userPrincipal.GetAuthorizationGroups();
<span style="color:Blue; if (groups != <span style="color:Blue; null)
{
<span style="color:Blue; foreach (GroupPrincipal l <span style="color:Blue; in groups)
{
Console.WriteLine(userName);
<span style="color:Blue; foreach (GroupPrincipal g <span style="color:Blue; in groups)
{
Console.WriteLine(g.DisplayName);
}
}
}
}
Console.ReadLine();
}
}
}
[/code]
View the full article