Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>davidg was on the right track, and I am crediting him with the answer.</p> <p>But the WMI query necessary was a little less than straightfoward, since I needed not just a list of users for the whole machine, but the subset of users <em>and groups</em>, whether local or domain, that were members of the local Administrators group. For the record, that WMI query was:</p> <pre><code>SELECT PartComponent FROM Win32_GroupUser WHERE GroupComponent = "Win32_Group.Domain='thehostname',Name='thegroupname'" </code></pre> <p>Here's the full code snippet:</p> <pre><code>public string GroupMembers(string targethost, string groupname, string targetusername, string targetpassword) { StringBuilder result = new StringBuilder(); try { ConnectionOptions Conn = new ConnectionOptions(); if (targethost != Environment.MachineName) //WMI errors if creds given for localhost { Conn.Username = targetusername; //can be null Conn.Password = targetpassword; //can be null } Conn.Timeout = TimeSpan.FromSeconds(2); ManagementScope scope = new ManagementScope("\\\\" + targethost + "\\root\\cimv2", Conn); scope.Connect(); StringBuilder qs = new StringBuilder(); qs.Append("SELECT PartComponent FROM Win32_GroupUser WHERE GroupComponent = \"Win32_Group.Domain='"); qs.Append(targethost); qs.Append("',Name='"); qs.Append(groupname); qs.AppendLine("'\""); ObjectQuery query = new ObjectQuery(qs.ToString()); ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query); ManagementObjectCollection queryCollection = searcher.Get(); foreach (ManagementObject m in queryCollection) { ManagementPath path = new ManagementPath(m["PartComponent"].ToString()); { String[] names = path.RelativePath.Split(','); result.Append(names[0].Substring(names[0].IndexOf("=") + 1).Replace("\"", " ").Trim() + "\\"); result.AppendLine(names[1].Substring(names[1].IndexOf("=") + 1).Replace("\"", " ").Trim()); } } return result.ToString(); } catch (Exception e) { Console.WriteLine("Error. Message: " + e.Message); return "fail"; } } </code></pre> <p>So, if I invoke Groupmembers("Server1", "Administrators", "myusername", "mypassword"); I get a single string returned with: </p> <p>SERVER1\Administrator<br> MYDOMAIN\Domain Admins </p> <p>The actual WMI return is more like this:</p> <p>\\SERVER1\root\cimv2:Win32_UserAccount.Domain="SERVER1",Name="Administrator"</p> <p>... so as you can see, I had to do a little string manipulation to pretty it up.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload