1

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.

Jirayia
  • 73
  • 1
  • 8

2 Answers2

1

@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)

Jirayia
  • 73
  • 1
  • 8
0

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();
}
Edper
  • 9,144
  • 1
  • 27
  • 46
  • Hey, Thanks for your answer. Do i need to add any kind of reference for this? i´m asking cause my query don´t have a Where()-method. – Jirayia Jul 23 '14 at 12:25
  • `System.Data.Entity` for reference and `System.Data.Objects` for Namespace. – Edper Jul 23 '14 at 12:27
  • Okay nice thanks alot. Now i have the problem, that i have multiple references on ObjectQuery. I use the System.Management package for a Management-scope. So now my ObjectQuery could be System.Management.ObjectQuery or System.Data.Objects.ObjectQuery. PS: sorry if that are some dumb questions i´m not a very skilled developer :S (just a trainee ^^) – Jirayia Jul 23 '14 at 12:50
  • @Jirayia My bad. Remove `System.Data.Objects` reference and `System.Data.Entity` namespace. See my updated answer above. – Edper Jul 23 '14 at 13:17
  • Hey, I think you missunderstood me. I want to get all Groups a special user is in... so i need to get all groups and filter them by a User not by a group. – Jirayia Jul 24 '14 at 09:52