开发者

Getting the list LocalAdmins for a set of Server

开发者 https://www.devze.com 2022-12-29 04:32 出处:网络
I have a list of computers (s开发者_如何学Ctored 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 have a list of computers (s开发者_如何学Ctored 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消