Getting the LocalAdmins List for a Set of Servers
I have a list of computers (stored in a database) and I want to programmatically define the local computers on those computers so that I can store this information in the database.
I understand it can be done using powershell. But looking for a way to do the same using C #
how to do it
+2
a source to share
1 answer
I just tried this code on my local machine and it works great:
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 test it on remote computers until I get back to work, but let's assume you're running an account that has permissions on the remote computer. I think it will work.
+3
a source to share