By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,929 Members | 634 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,929 IT Pros & Developers. It's quick & easy.

Getting local admin groups and users on a windows server using ADSI

P: n/a
hi all,
i am trying to get local admin users and groups on a windows server.
here is the C# code that i am using
************************************************** *************************************************
DirectoryEntry AD = new DirectoryEntry("WinNT://" + sServerName +
",Computer",UserName,Password);
DirectoryEntry admGroup = AD.Children.Find("Administrators", "Group");
object members = admGroup.Invoke("Members", null);

foreach(object member in (System.Collections.IEnumerable)members)
{
DirectoryEntry user_groups = new DirectoryEntry(member);
// do some work.
}
************************************************** ************************************************** **

I have 2 issues here.
1. Its not getting all the local users and groups from the
Administrators group on the box.
2. How does it know which domain to look for with just "WinNT://" +
servername.... i have 2 domains, test and production and both domains
have same server names. i.e there is a AppServer in each of the test
and production domain.
3. Is there a different syntax to tell the DirectoryEntry object to
look at a particular server on a particular domain?

thanks,
shashank kadge

Mar 7 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"shashank kadge" <ma**********@gmail.comwrote in message
news:11**********************@t69g2000cwt.googlegr oups.com...
hi all,
i am trying to get local admin users and groups on a windows server.
here is the C# code that i am using
************************************************** *************************************************
DirectoryEntry AD = new DirectoryEntry("WinNT://" + sServerName +
",Computer",UserName,Password);
DirectoryEntry admGroup = AD.Children.Find("Administrators", "Group");
object members = admGroup.Invoke("Members", null);

foreach(object member in (System.Collections.IEnumerable)members)
{
DirectoryEntry user_groups = new DirectoryEntry(member);
// do some work.
}
************************************************** ************************************************** **

I have 2 issues here.
1. Its not getting all the local users and groups from the
Administrators group on the box.
2. How does it know which domain to look for with just "WinNT://" +
servername.... i have 2 domains, test and production and both domains
have same server names. i.e there is a AppServer in each of the test
and production domain.
3. Is there a different syntax to tell the DirectoryEntry object to
look at a particular server on a particular domain?
All your questions are pretty well explained in the DirectoryEntry description in the MSDN
docs, search for "DirectoryEntry" and follow the "Path" property description.

Willy.

Mar 7 '07 #2

P: n/a
On Mar 7, 2:48 pm, "Willy Denoyette [MVP]"
<willy.denoye...@telenet.bewrote:
"shashank kadge" <mailtosha...@gmail.comwrote in message

news:11**********************@t69g2000cwt.googlegr oups.com...


hi all,
i am trying to get local admin users and groups on a windows server.
here is the C# code that i am using
************************************************** **************************************************
DirectoryEntry AD = new DirectoryEntry("WinNT://" + sServerName +
",Computer",UserName,Password);
DirectoryEntry admGroup = AD.Children.Find("Administrators", "Group");
object members = admGroup.Invoke("Members", null);
foreach(object member in (System.Collections.IEnumerable)members)
{
DirectoryEntry user_groups = new DirectoryEntry(member);
// do some work.
}
************************************************** ************************************************** ***
I have 2 issues here.
1. Its not getting all the local users and groups from the
Administrators group on the box.
2. How does it know which domain to look for with just "WinNT://" +
servername.... i have 2 domains, test and production and both domains
have same server names. i.e there is a AppServer in each of the test
and production domain.
3. Is there a different syntax to tell the DirectoryEntry object to
look at a particular server on a particular domain?

All your questions are pretty well explained in the DirectoryEntry description in the MSDN
docs, search for "DirectoryEntry" and follow the "Path" property description.

Willy.- Hide quoted text -

- Show quoted text -
thanks for the link.
i tried using the syntax on msdn but no luck.
i would appreciate if some1 can get some code or suggest a
modification on my code.

thanks,
shashank kadge

Mar 7 '07 #3

P: n/a
"shashank kadge" <ma**********@gmail.comwrote in message
news:11**********************@s48g2000cws.googlegr oups.com...
On Mar 7, 2:48 pm, "Willy Denoyette [MVP]"
<willy.denoye...@telenet.bewrote:
"shashank kadge" <mailtosha...@gmail.comwrote in message

news:11**********************@t69g2000cwt.googlegr oups.com...


hi all,
i am trying to get local admin users and groups on a windows server.
here is the C# code that i am using
************************************************** **************************************************
DirectoryEntry AD = new DirectoryEntry("WinNT://" + sServerName +
",Computer",UserName,Password);
DirectoryEntry admGroup = AD.Children.Find("Administrators", "Group");
object members = admGroup.Invoke("Members", null);
foreach(object member in (System.Collections.IEnumerable)members)
{
DirectoryEntry user_groups = new DirectoryEntry(member);
// do some work.
}
************************************************** ************************************************** ***
I have 2 issues here.
1. Its not getting all the local users and groups from the
Administrators group on the box.
2. How does it know which domain to look for with just "WinNT://" +
servername.... i have 2 domains, test and production and both domains
have same server names. i.e there is a AppServer in each of the test
and production domain.
3. Is there a different syntax to tell the DirectoryEntry object to
look at a particular server on a particular domain?

All your questions are pretty well explained in the DirectoryEntry description in the MSDN
docs, search for "DirectoryEntry" and follow the "Path" property description.

Willy.- Hide quoted text -

- Show quoted text -
thanks for the link.
i tried using the syntax on msdn but no luck.
i would appreciate if some1 can get some code or suggest a
modification on my code.

thanks,
shashank kadge

Because you have the same machine names in both domains, you'll have to set the IP address
instead of the machine name in the path.
And because you are binding using the WinNT provider, you'll have to set a reference to
activeds.tlb (%Windir%system32)
Here's a small sample...

...
string groupName = "administrators";
IADsMembers MembersCollection = null;
using(DirectoryEntry groupEntry = new DirectoryEntry("WinNT://xxx.xxx.xxx.xxx/" +
groupName + ",group","Administrator", "pwd",
AuthenticationTypes.Secure))
{
// invoke native method "members"
MembersCollection = groupEntry.Invoke("Members") as IADsMembers;
object[] filter = {"User", "Group"};
MembersCollection.Filter = filter;
foreach (object obj in MembersCollection)
{
IADsGroup group = obj as IADsGroup;
if(group != null)
Console.WriteLine("group Name: {0}", group.Name);
//this is a group, so recurse to get the members of this group....
else
{
IADsUser user = obj as IADsUser;
Console.WriteLine("User Name: {0}", user.Name );
}
}
}
Willy.

Mar 7 '07 #4

P: n/a
On Mar 7, 5:26 pm, "Willy Denoyette [MVP]"
<willy.denoye...@telenet.bewrote:
"shashank kadge" <mailtosha...@gmail.comwrote in message

news:11**********************@s48g2000cws.googlegr oups.com...
On Mar 7, 2:48 pm, "Willy Denoyette [MVP]"

<willy.denoye...@telenet.bewrote:
"shashank kadge" <mailtosha...@gmail.comwrote in message
news:11**********************@t69g2000cwt.googlegr oups.com...
hi all,
i am trying to get local admin users and groups on a windows server.
here is the C# code that i am using
************************************************** ************************************************** *
DirectoryEntry AD = new DirectoryEntry("WinNT://" + sServerName +
",Computer",UserName,Password);
DirectoryEntry admGroup = AD.Children.Find("Administrators", "Group");
object members = admGroup.Invoke("Members", null);
foreach(object member in (System.Collections.IEnumerable)members)
{
DirectoryEntry user_groups = new DirectoryEntry(member);
// do some work.
}
************************************************** ************************************************** ****
I have 2 issues here.
1. Its not getting all the local users and groups from the
Administrators group on the box.
2. How does it know which domain to look for with just "WinNT://" +
servername.... i have 2 domains, test and production and both domains
have same server names. i.e there is a AppServer in each of the test
and production domain.
3. Is there a different syntax to tell the DirectoryEntry object to
look at a particular server on a particular domain?
All your questions are pretty well explained in the DirectoryEntry description in the MSDN
docs, search for "DirectoryEntry" and follow the "Path" property description.
Willy.- Hide quoted text -
- Show quoted text -

thanks for the link.
i tried using the syntax on msdn but no luck.
i would appreciate if some1 can get some code or suggest a
modification on my code.

thanks,
shashank kadge

Because you have the same machine names in both domains, you'll have to set the IP address
instead of the machine name in the path.
And because you are binding using the WinNT provider, you'll have to set a reference to
activeds.tlb (%Windir%system32)
Here's a small sample...

..
string groupName = "administrators";
IADsMembers MembersCollection = null;
using(DirectoryEntry groupEntry = new DirectoryEntry("WinNT://xxx.xxx..xxx.xxx/" +
groupName + ",group","Administrator", "pwd",
AuthenticationTypes.Secure))
{
// invoke native method "members"
MembersCollection = groupEntry.Invoke("Members") as IADsMembers;
object[] filter = {"User", "Group"};
MembersCollection.Filter = filter;
foreach (object obj in MembersCollection)
{
IADsGroup group = obj as IADsGroup;
if(group != null)
Console.WriteLine("group Name: {0}", group.Name);
//this is a group, so recurse to get the members of this group....
else
{
IADsUser user = obj as IADsUser;
Console.WriteLine("User Name: {0}", user.Name );
}
}
}

Willy.- Hide quoted text -

- Show quoted text -
Yeah, that helps.
Thanks for all the help Willy.

-
shashank kadge

Mar 8 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.