How can i get all groups of a local user using ObjectQuery?

1.2k views Asked by At

i try to display all groups a special User is in. I also know, that i could do it like this:

 public static List<Principal> getUsers(){
     PrincipalContext context = new PrincipalContext(ContextType.Machine, "computername");
        PrincipalSearcher search = new PrincipalSearcher(new UserPrincipal(context));
        return search.FindAll().ToList();

    }

But i want to work arount PrincipalContext because i need to Use this remotely on a PC, wich is in no Domain. So i tried this:

 public static void findUsers()
    {
        ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_Group WHERE LocalAccount.Name =\'Test'");
        ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
        var result = searcher.Get();
         foreach (var envVar in result)
        {
            Console.WriteLine("GroupName: {0}", envVar["Name"]);
        }
        Console.ReadLine();
    }

It gives me an Exception because the query isnĀ“t correct.

Thanks alot for any kind of help.

2

There are 2 answers

1
Jirayia On BEST ANSWER

@Edper your tips were very nice but i used another way to solve my problem.

the mission was to just enter a username and an IP of a remote-Server and u get all Groups this local user is in.

class Program
{
    static ManagementScope scope =
           new ManagementScope(
               "\\\\ServerIP\\root\\cimv2");
    static string username = "Test";


    static void Main(string[] args)
    {
        string partComponent = "Win32_UserAccount.Domain='Domain',Name='"+username+"'";
        ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_GroupUser WHERE PartComponent = \"" + partComponent + "\"");
        using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query))
        {
            var result = searcher.Get();
            foreach (var envVar in result)
            {
                ManagementObject groupComponent = new ManagementObject("\\\\ServerIP\\root\\cimv2", envVar["GroupComponent"].ToString(), null);
                Console.WriteLine(groupComponent["Name"]);
            }
        }
        Console.ReadLine(); 
    }
}

of course this is not done jet(GUI in progress) but it does all i want for now.

if you want to test it you need to make a local user on the remote PC that has got the same username and Password as the User u run the Code with.(and this user needs admin rights)

5
Edper On

There is no LocalAccount.Name field instead just use simply Name and remove also \, so that it would look like: (I used 'Guests' as my example not 'Test'

public static void findUsers()
{
    ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_Group WHERE Name = 'Guests'");
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
    var result = searcher.Get();
     foreach (var envVar in result)
    {
        Console.WriteLine("GroupName: {0}", envVar["Name"]);
    }
    Console.ReadLine();
}