tags:

views:

29

answers:

1

I have a list of computers (stored in a database) and I want to find out the localadmins on those computers programmatically so that I can store that information in the database too.

I understand this can be done using powershell. But looking for a way to do the same thing using C#

how do I do that

A: 

I've just tried this code on my local computer and it works fine:

        string hostName = "myComputer";
        //get machine
        using (DirectoryEntry machine = new DirectoryEntry("WinNT://" + hostName))
        {
            //get local admin group
            using (DirectoryEntry group = machine.Children.Find("Administrators", "Group"))
            {
                //get all members of local admin group
                object members = group.Invoke("Members", null);
                foreach (object member in (IEnumerable)members)
                {
                    //get account name
                    string accountName = new DirectoryEntry(member).Name;
                    //DO SOMETHING...
                }
            }        
        }

I can't check it on remote computers until I get back to work but presuming you're running under an account that has permissions on the remote computer I should imagine it will work.

David Neale
Thanks David for the response. So, this will give the list of any group member on the local machine..right? I'm trying to get the LocalAdmins on a list of remote machines. I want to run this program from my desktop and get the list of localadmins on a list of remote machines in the same domain and a trusting domain.
PK
Amended as above.
David Neale
Yes.. This works great. I just needed to add -- using system.collections.generic at the top. Thank you very much..
PK