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.
精彩评论