467,173 Members | 1,330 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,173 developers. It's quick & easy.

ADSI Query to filter out machine accounts in the domain

I have the following C# code to enumerate the list of groups in a
domain using ADSI. The problem is if the domain contains machine
accounts ($) it get those accounts too.

Can somebody help me here to filter out the machine accounts so that I
get only the NT group objects.

public ArrayList GetNTGroups()
{
DirectoryEntry ntDirectoryGroups = null;
try
{
ntDirectoryGroups = new DirectoryEntry(bindNTDomainPath,
bindNTUser, bindNTPassword);
ArrayList groupsArray = new ArrayList();
foreach(DirectoryEntry group in ntDirectoryGroups.Children)
{
switch(group.SchemaClassName.ToLower())
{
case "group" :
groupsArray.Add(group.Name);
break;
default :
break;
}
}
groupsArray.Sort();
return groupsArray;
}
catch(COMException ex)
{
return null;
}
finally
{
ntDirectoryGroups.Dispose();
}
}

Thanks,

Prasad
Nov 15 '05 #1
  • viewed: 3834
Share:
3 Replies
Please specify what domain NT or AD.

Willy.

"Prasad Karunakaran" <pr*******@hotmail.com> wrote in message
news:87**************************@posting.google.c om...
I have the following C# code to enumerate the list of groups in a
domain using ADSI. The problem is if the domain contains machine
accounts ($) it get those accounts too.

Can somebody help me here to filter out the machine accounts so that I
get only the NT group objects.

public ArrayList GetNTGroups()
{
DirectoryEntry ntDirectoryGroups = null;
try
{
ntDirectoryGroups = new DirectoryEntry(bindNTDomainPath,
bindNTUser, bindNTPassword);
ArrayList groupsArray = new ArrayList();
foreach(DirectoryEntry group in ntDirectoryGroups.Children)
{
switch(group.SchemaClassName.ToLower())
{
case "group" :
groupsArray.Add(group.Name);
break;
default :
break;
}
}
groupsArray.Sort();
return groupsArray;
}
catch(COMException ex)
{
return null;
}
finally
{
ntDirectoryGroups.Dispose();
}
}

Thanks,

Prasad

Nov 15 '05 #2
Willy,
It is an Active Directory domain. Thanks for your help.

regards,

Prasad

"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message news:<uh*************@TK2MSFTNGP10.phx.gbl>...
Please specify what domain NT or AD.

Willy.

"Prasad Karunakaran" <pr*******@hotmail.com> wrote in message
news:87**************************@posting.google.c om...
I have the following C# code to enumerate the list of groups in a
domain using ADSI. The problem is if the domain contains machine
accounts ($) it get those accounts too.

Can somebody help me here to filter out the machine accounts so that I
get only the NT group objects.

public ArrayList GetNTGroups()
{
DirectoryEntry ntDirectoryGroups = null;
try
{
ntDirectoryGroups = new DirectoryEntry(bindNTDomainPath,
bindNTUser, bindNTPassword);
ArrayList groupsArray = new ArrayList();
foreach(DirectoryEntry group in ntDirectoryGroups.Children)
{
switch(group.SchemaClassName.ToLower())
{
case "group" :
groupsArray.Add(group.Name);
break;
default :
break;
}
}
groupsArray.Sort();
return groupsArray;
}
catch(COMException ex)
{
return null;
}
finally
{
ntDirectoryGroups.Dispose();
}
}

Thanks,

Prasad

Nov 15 '05 #3
Ok just to be sure :-)

Use a directorySearcher with a filter...

ntDirectoryGroups = new DirectoryEntry(bindNTDomainPath,bindNTUser,
bindNTPassword);

src = new DirectorySearcher();
// specify properties to load
string[] props = {"cn", more properties};
src.PropertiesToLoad.AddRange(props);
src.SearchRoot = ntDirectoryGroups;
src.SearchScope = SearchScope.Subtree;
// return all groups except "domain computers" and "domain controllers" and
.......
src.Filter = "(&(objectCategory=group)(!cn=domain computers)(!cn=domain
controllers))";
SearchResultCollection res = src.FindAll();
// process the objects in the collection
foreach(SearchResult sc in res) {
.....

Willy.
"Prasad Karunakaran" <pr*******@hotmail.com> wrote in message
news:87**************************@posting.google.c om...
Willy,
It is an Active Directory domain. Thanks for your help.

regards,

Prasad

"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:<uh*************@TK2MSFTNGP10.phx.gbl>...
Please specify what domain NT or AD.

Willy.

"Prasad Karunakaran" <pr*******@hotmail.com> wrote in message
news:87**************************@posting.google.c om...
>I have the following C# code to enumerate the list of groups in a
> domain using ADSI. The problem is if the domain contains machine
> accounts ($) it get those accounts too.
>
> Can somebody help me here to filter out the machine accounts so that I
> get only the NT group objects.
>
> public ArrayList GetNTGroups()
> {
> DirectoryEntry ntDirectoryGroups = null;
> try
> {
> ntDirectoryGroups = new DirectoryEntry(bindNTDomainPath,
> bindNTUser, bindNTPassword);
> ArrayList groupsArray = new ArrayList();
> foreach(DirectoryEntry group in ntDirectoryGroups.Children)
> {
> switch(group.SchemaClassName.ToLower())
> {
> case "group" :
> groupsArray.Add(group.Name);
> break;
> default :
> break;
> }
> }
> groupsArray.Sort();
> return groupsArray;
> }
> catch(COMException ex)
> {
> return null;
> }
> finally
> {
> ntDirectoryGroups.Dispose();
> }
> }
>
> Thanks,
>
> Prasad

Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by Arran Pearce | last post: by
2 posts views Thread by YRao | last post: by
3 posts views Thread by Roy Osherove | last post: by
5 posts views Thread by Tim::.. | last post: by
14 posts views Thread by Jonathan Smith | last post: by
1 post views Thread by LittlePython | last post: by
8 posts views Thread by John | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.