views:

17

answers:

1

i have the code which find all local users:

        ManagementObjectSearcher userSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_UserAccount");
        foreach (ManagementObject user in userSearcher.Get())
        {
            if ((bool)user["LocalAccount"])
            {
                string UserName = (string)user["Name"];
            }
        }
        return;

now i want code which help me to select all groups in which user consist. I know that there is table called Win32_GroupUser and i must use PartComponent to indicate user name but i cant create query. Please help with information about WQL.

A: 

The query will look something like:

select * from Win32_GroupUser 
       WHERE PartComponent="Win32_Group.Domain='Workgroup',Name='LocalAccount'" 

string query = "Select * from Win32_GroupUser Where PartComponent=";
query += '"Win32_Group.Domain=';
query += "'" + user["Domain"] + "'";
query += ",Name='" + user["Name"] + "'";
query += '"';

You need to pass the domain name and username to lookup the associated groups. For performance reasons I would recommend StringBuilder class.

I derived this answer from this article.

Jack B Nimble