Willy Denoyette [MVP]
7/17/2002 1:41:00 PM
"member" is not a property of the group entry.
To list the members in a group entry, you need to call the native Activeds COM IADsGroup interface method "Members".
This requires a reference to activeds.tlb, or run tlbimp.exe against activeds.tlb .
Here is a sample:
using System;
using System.DirectoryServices;
using System.Runtime.InteropServices;
using System.Reflection;
using activeds; // Import activeds.tlb (%windir%\system32\activeds.tlb)
class Test {
static string bindUser = "administrator"; // binding user with sufficient privileges
static string bindPwd = "hispwd"; // password of the binding user
static void ListUserAndGroups(string machineName)
{
DirectoryEntry _compContainer = new DirectoryEntry("WinNT://" + machineName + ",computer", bindUser, bindPwd);
try
{
foreach(DirectoryEntry de in _compContainer.Children)
{
switch (de.SchemaClassName.ToLower())
{
case "group":
Console.WriteLine("---------- group - {0} ---------", de.Name);
ListMembersInGroup(de.Path);
break;
case "user":
Console.WriteLine("---------- user - {0} ---------", de.Name);
ListUserProp(de.Path);
break;
default:
break;
}
}
}
finally {
_compContainer.Dispose();
}
}
private static void ListMembersInGroup(string dirPath) {
IADsMembers MembersCollection = null;
DirectoryEntry _groupEntry = new DirectoryEntry(dirPath ,bindUser, bindPwd);
try {
// call native method "members" on the IADsGroup COM interface exposed by activeds.dll
IADsGroup gr = _groupEntry.NativeObject as IADsGroup;
MembersCollection = gr.Members();
// or call Invoke on the DirectoryEntry object passing the Method to call as arg.
// cast the retruned object to IADsMembers
// MembersCollection = _groupEntry.Invoke("Members") as IADsMembers;
object[] filter = {"user"};
MembersCollection.Filter = filter;
// enumerate members of collection object that supports the IADsMembers interface
// ADSI provider doesn't support count property!!
try {
foreach (IADsUser member in MembersCollection) {
Console.WriteLine("[{0}]", member.Name);
ListUserProp(member.ADsPath);
}
}
catch (COMException e) {
Console.WriteLine("Error: {0}",e.Message);
}
}
catch (COMException e) {
Console.WriteLine(e.Message);
}
finally {
_groupEntry.Dispose();
}
}
private static void ListUserProp(string dirPath) {
DirectoryEntry userEntry = null;
try {
userEntry = new DirectoryEntry(dirPath,bindUser, bindPwd);
PropertyCollection pcoll = userEntry.Properties;
foreach(string sc in pcoll.PropertyNames)
Console.WriteLine("\t" + sc + "\t" + pcoll[sc].Value);
}
catch (COMException e) {
Console.WriteLine(e.Message);
}
finally
{
userEntry.Dispose();
}
}
public static void Main() {
ListUserAndGroups("scenic");
}
}
Willy.
"Robert Chartier" <rob-nospam@santra.com> wrote in message news:O74ILkRLCHA.2312@tkmsftngp13...
> WinNt provider again.
>
> AD would represent the computer
>
> WinNT://MachineName,computer
>
>
> i can list groups fine, but cannot get users based on a group, or groups
> based on a user.
>
> ive also tried
>
> foreach(object o in sr.Properties["member"]){}
>
> with no luck
>
> thanks for any input
>
>
>
> "Willy Denoyette [MVP]" <willy.denoyette@pandora.be> wrote in message
> news:#CahmNRLCHA.1332@tkmsftngp04...
> > What does AD represent in this snippet?
> >
> > Please post a complete repro if possible.
> >
> > Willy.
> >
> > "Robert Chartier" <rob-nospam@aspfree.com> wrote in message
> news:OrRy$#FKCHA.2688@tkmsftngp11...
> > > AD is correctly loaded and works (for some things -see my previous
> emails) ,
> > > but im trying to get a list of the Children (assuming users, etc..) of a
> > > group:
> > >
> > > string groupname = "Administrators";
> > >
> > > System.Collections.ArrayList users = new System.Collections.ArrayList();
> > > foreach(System.DirectoryServices.DirectoryEntry dir in
> > > AD.Children.Find(groupname, "group").Children) {
> > > if(dir.SchemaClassName=="User") users.Add(dir);
> > > }
> > >
> > > Assuming this is the correct method for listing users that belong to a
> > > group, it doenst enter the foreach at all.
> > >
> > > security is not an issue because im impersontating an administrator
> > >
> > > pardon my rudeness/frusteration, but...
> > >
> > > Can some explain to me why me, and a few of my collegues here are having
> so
> > > many problems with this namespace? Also why im not getting any response
> > > from anyone on this? (5+ hours is long enough to wait, inst it?) Was
> this
> > > namespace added/hacked in (via interop) just before launch and not
> tested
> > > approriately? Will Microsoft be fixing this issue soon? What do they
> > > recommend for doing this now? Should I stop wasting my time with this
> > > version of Interop (System.DirectoryServices) and do it all my self?
> What
> > > are my alternatives?
> > >
> > > Is the code posted in this email, and the two others correct? If not,
> can
> > > you please provide sample code for each request, which actually works.
> > >
> > > Any response indicating any sort of action being taken will be
> appreciated.
> > > My publisher is demanding that I have this article done for today so I
> will
> > > be forced to send this incomplete article, indicating these issues and
> the
> > > problems in the framework itself.
> > >
> > > I do not mind sending my code to you to test with. I have singed a NDA
> with
> > > Microsoft, so if this list is inappropriate please contact me directly
> at
> > > rob@santra.com
> > >
> > >
> > > Thanks for your immediate attention.
> > >
> > >
> > >
> > >
> >
> >
>
>