PC Review


Reply
Thread Tools Rate Thread

ADSI Query to filter out machine accounts in the domain

 
 
Prasad Karunakaran
Guest
Posts: n/a
 
      5th Mar 2004
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
 
Reply With Quote
 
 
 
 
Willy Denoyette [MVP]
Guest
Posts: n/a
 
      5th Mar 2004
Please specify what domain NT or AD.

Willy.

"Prasad Karunakaran" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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



 
Reply With Quote
 
Prasad Karunakaran
Guest
Posts: n/a
 
      7th Mar 2004
Willy,
It is an Active Directory domain. Thanks for your help.

regards,

Prasad

"Willy Denoyette [MVP]" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> Please specify what domain NT or AD.
>
> Willy.
>
> "Prasad Karunakaran" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> >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

 
Reply With Quote
 
Willy Denoyette [MVP]
Guest
Posts: n/a
 
      7th Mar 2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Willy,
> It is an Active Directory domain. Thanks for your help.
>
> regards,
>
> Prasad
>
> "Willy Denoyette [MVP]" <(E-Mail Removed)> wrote in message
> news:<(E-Mail Removed)>...
>> Please specify what domain NT or AD.
>>
>> Willy.
>>
>> "Prasad Karunakaran" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>> >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



 
Reply With Quote
 
Marc Scheuner [MVP ADSI]
Guest
Posts: n/a
 
      8th Mar 2004
> 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))";



You probably didn't really mean to specify !cn= filter, right?
Shouldn't that be !objectCategory= instead??

src.Filter = "(&(objectCategory=group)(!objectCategory=domain
computers)(!objectCategory=domain controllers))";

Marc
================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
 
Reply With Quote
 
Willy Denoyette [MVP]
Guest
Posts: n/a
 
      8th Mar 2004
Marc,
Not really, "DomainCoputers" and "Domain controllers" are no
objectCategories.
"computer" is a objectCategory but this won't help to filter on this when
searching on 'group' as they are part of container (just like group).

Willy.

"Marc Scheuner [MVP ADSI]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>> 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))";

>
>
> You probably didn't really mean to specify !cn= filter, right?
> Shouldn't that be !objectCategory= instead??
>
> src.Filter = "(&(objectCategory=group)(!objectCategory=domain
> computers)(!objectCategory=domain controllers))";
>
> Marc
> ================================================================
> Marc Scheuner May The Source Be With You!
> Bern, Switzerland m.scheuner(at)inova.ch



 
Reply With Quote
 
Marc Scheuner [MVP ADSI]
Guest
Posts: n/a
 
      8th Mar 2004
>Not really, "DomainCoputers" and "Domain controllers" are no
>objectCategories.


Okay - there is one for server or domain controllers, no?

Then - what good does the !cn=domain controllers filter really do?
Will this exclude all the subobjects in that container?

Marc
================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
 
Reply With Quote
 
Willy Denoyette [MVP]
Guest
Posts: n/a
 
      8th Mar 2004
Marc,
No the objectCategories are:
builtinDomain, computer, contact, container, domain, domainDNS,
lostAndFound,
group,organizationalUnit, organizationalPerson, person, secret, user and a
few more which I can't remember for now), but "domain controllers" and
"domain computers" are account groups ; respectively - 'security enabled
universal group' and 'security enabled account group' types (see, there is
no difference between computer accounts and user accounts in AD).
So, when using group as filter all group types will be returned in the
collection, to exclude some group types you will have to use some constraint
based on a group property like cn.

When using "(&(objectCategory=group)(!cn=domain computers)(!cn=domain
controllers))"; as filter the SearchResultCollection will contain all groups
except the groups cn=domain computers and cn=domain controller.

Willy.


"Marc Scheuner [MVP ADSI]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> >Not really, "DomainCoputers" and "Domain controllers" are no
>>objectCategories.

>
> Okay - there is one for server or domain controllers, no?
>
> Then - what good does the !cn=domain controllers filter really do?
> Will this exclude all the subobjects in that container?
>
> Marc
> ================================================================
> Marc Scheuner May The Source Be With You!
> Bern, Switzerland m.scheuner(at)inova.ch



 
Reply With Quote
 
Prasad Karunakaran
Guest
Posts: n/a
 
      11th Mar 2004
Willy,
I tried the code you proposed. The code works great with AD. I use
the LDAP provider ("LDAP://DomainName") in the domain path when I
create the DirectoryEntry() object.

I tried using the same code for a NT domain. I changed the provider to
WinNT
("WinNT://DomainName") in the domain path and use a domain user
account while creating DirectoryEntry() object

I get an expection in the following line

SearchResultCollection res = src.FindAll()

The exception message : "The provider does not support searching and
cannot search WinNT://corp.aspentech.com"
Error Code : 0x80131515

How should I change the code to work for a NT domain ?. Basically I
have to support enumerating NT groups (expect machine accounts) in
both NT Domain and AD domain. As you know WinNT provider is supported
in AD for backward compatibility.


Thanks for your help.

regards,

Prasad
 
Reply With Quote
 
Marc Scheuner [MVP ADSI]
Guest
Posts: n/a
 
      12th Mar 2004
> I tried the code you proposed. The code works great with AD. I use
>the LDAP provider ("LDAP://DomainName") in the domain path when I
>create the DirectoryEntry() object.
>
>I tried using the same code for a NT domain. I changed the provider to
>WinNT ("WinNT://DomainName") in the domain path and use a domain user
>account while creating DirectoryEntry() object
>
>I get an expection in the following line
>The exception message : "The provider does not support searching and
>cannot search WinNT://corp.aspentech.com"
>Error Code : 0x80131515
>
>How should I change the code to work for a NT domain ?.


In short - you can't.

From the MSDN docs:

"Use a DirectorySearcher to search and perform queries against an
Active Directory hierarchy using the Lightweight Directory Access
Protocol (LDAP). LDAP is the only system-supplied Active Directory
Service Interfaces (ADSI) provider that supports directory searching."

DirectorySearcher does *NOT* support WinNT.

Marc
================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Domain Server Error: The Query Could not be processed. Safety settings on this machine prohibit accessing a data source on another domain aSoundMind Microsoft Frontpage 4 24th Apr 2007 03:30 AM
Restrict both local machine accounts and domain accounts from login Tekmazter Microsoft Windows 2000 Security 3 17th Nov 2005 04:32 PM
Check Active Directory Domain is ADVERTISED using ADSI Manu Microsoft Windows 2000 Active Directory 0 7th Sep 2005 12:08 PM
ADSI local machine administrators members? =?Utf-8?B?bGFzdHVzZXJuYW1lbGVmdA==?= Microsoft Dot NET 0 3rd Jun 2005 09:53 PM
Duplicate Domain entries in ADSI Edit Debbie B. Microsoft Windows 2000 Active Directory 1 9th Apr 2004 12:57 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:12 AM.