Enumerate Local Admin Group

I'm new to Directory Services. I am trying to convert this vbscript to C#. Can anyone give me an example of how to enumerate the Local Administrators group on a WIN2K machine w/ Directory Services. I beleive I need a Searcher, but I can not figure out how to use it...We do not have Active Directory installed on our Domain Controllers but we do have ADSI 2.5 installed on our WIN2K boxes. The vbscript below successfully gives me every domain account assigned to the Local Admin group. If anyone can just help me enumerate the local admin group I would be extremely pleased. I would even be open to using System.Management and WMI...

-------------------------------------------------------------------------------------------------
strComputer = "."
Set colGroups = GetObject("WinNT://" & strComputer & "/" & "Administrators")
For Each objUser in colGroups.Members
       
If objUser.Class = "User" Then

     On Error Resume Next
     'try to connect to user object to see if account is a local user  
     Set oUser = GetObject("WinNT://" & strComputer & "/" & objUser.Name & ",user")

     If Err.Number <> 0 Then
      'user is not local therefore output to screen
       Wscript.Echo objUser.Name
     End If

End If
Next
-------------------------------------------------------------------------------------------------
//What I have so far

string sNode = System.Environment.UserDomainName;
string sGroupName = "Administrators";
string strEntry = "WinNT://" + sNode + "/" + sGroupName;
DirectoryEntry myEntry = new DirectoryEntry(strEntry);
acousticsateliteAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ihenryCommented:
It can be done using .NET S.DS API with the WinNT provider. Like so,

using System.DirectoryServices;

string adsPath = "WinNT://theComputerName/Users, Group";
using ( DirectoryEntry group = new DirectoryEntry(adsPath) )
{
      foreach (object o in (IEnumerable) group.Invoke("members") )
      {
            using ( DirectoryEntry user = new DirectoryEntry(o) )
            {
                  Console.WriteLine( "Name: {0}", user.Name );
                  Console.WriteLine( "Full name: {0}", user.Properties["fullName"].Value );
                  Console.WriteLine( "Path: {0}", user.Path );
                  Console.WriteLine( "------------------" );
            }
      }
}

Henry
0
acousticsateliteAuthor Commented:
ihenry,

This looks great, however I am only getting the local accounts for the machine returned. Do I need to modify the syntax in order to get domain level accounts which are members of the local admin group for that machine. I am just now gettting into Active Directory so Im sure its something in the /Users, Group"; area of the syntax that I need to modify
0
ihenryCommented:
When you're running the code, did you logon with local account or domain account?
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

acousticsateliteAuthor Commented:
Domain Account
0
ihenryCommented:
Like this?
string adsPath = "WinNT://theComputerName/Administrators, Group";
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
acousticsateliteAuthor Commented:
Great, that works ihenry. I did some string manipulation to get rid of the local accounts which where in the Local Admin Group. Now I am left only with Domain accounts, do you have any good Active Directory resources on the web. I think I can handle System.DirectoryServices, but I need to have a foundation in AD first.
0
ihenryCommented:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.