Sam Evans said:
Thank you for the reply, Willy.
From reading the ADSI information it seems like it is geared towards
enumerating information from Active Directory its self rather than having
anything to do with a local machine.
I'm trying to enumerate members of a group from a local machine (i.e.,
connecting to the local machine and finding out who the members are of its
local Administrators group).
Would the ADSI still work for me in this situation?
ADSI and the ADSI wrappers in System.DirectoryServices are not only
targetting LDAP servers (AD) but also the local security administrative
service & SAM database. This is done through two different client providers,
one is the LDAP provider the other is the WinNT provider. The first can (and
should ) be used when connecting to the AD (or any LDAP v2 compliant)
directory service. The latter can be used to connect to a NT4 domain (and AD
domain, but this is not advisable) or local server.
Here's a sample that enumerates a local alias:
private static void ListMembersInGroup2(string GroupName)
{
// Connect to a local server using the WinNT provider interface
using(DirectoryEntry groupEntry = new
DirectoryEntry("WinNT://YourMachineName/" + GroupName + ",group"))
{
object members = groupEntry.Invoke("Members");
foreach( object member in (IEnumerable) members)
{
DirectoryEntry x = new DirectoryEntry(member);
Console.WriteLine("ADsPath "+ x.Path + " Name: " + x.Name);
}
}
}
public static void Main()
{
ListMembersInGroup("Users"); // enum 'users' alias
}
And here a more elaborated sample. This one uses COM interop to access the
native ds classes from activeds.dll, so you need to add a reference to
activeds.tlb (in system32) and you have to add a using clause to import that
namespace.
private static void ListMembersInGroup2(string GroupName)
{
IADsMembers MembersCollection = null;
using(DirectoryEntry groupEntry = new DirectoryEntry("WinNT://acer1/" +
GroupName + ",group"))
{
// invoke native method "members"
MembersCollection = groupEntry.Invoke("Members") as IADsMembers;
object[] filter = {"User"}; // return only User objects
MembersCollection.Filter = filter;
foreach (IADsUser member in MembersCollection) {
DirectoryEntry x = new DirectoryEntry(member);
foreach(string s in x.Properties.PropertyNames)
{
Console.WriteLine("{0} \t\t {1}", s, x.Properties
.Value);
}
}
}
Willy.