title: WMI Flakiness / Enumerating User Accounts
body: <p>I am using WMI to enumerate users of the computer but with some customers I am getting cryptic exceptions and/or the code doesn't work as expected. For one customer NO user account information is returned from this simple code:</p>
  
  <pre><code>  SelectQuery query = new SelectQuery("Win32_UserAccount", string.Format("Domain='{0}'", Environment.MachineName));
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
    foreach (ManagementObject mObject in searcher.Get()) {
      Console.WriteLine((string)mObject["Name"]);
    }
  </code></pre>
  
  <p>But for the same customer, I am able to enumerate groups using this code which produces output like "Administradores", "Convidados", "Distributed COM - Usuários", etc.</p>
  
  <pre><code>  SelectQuery query = new SelectQuery("Win32_Group", string.Format("Domain='{0}'", Environment.MachineName));
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
    foreach (ManagementObject mObject in searcher.Get()) {
      Console.WriteLine((string)mObject["Name"]);
    }
  </code></pre>
  
  <p>Attempting to enumerate users in a particular group using the code below generates an exception. I am passing in Environment.MachineName as the domain name and a valid group name.</p>
  
  <pre><code>public static void GetUsersInGroup(string domainName, string groupName) {
    string findUsersQueryText = string.Format("GroupComponent=\"Win32_Group.Domain='{0}',Name='{1}'\"", domainName, groupName);
    SelectQuery query = new SelectQuery("Win32_GroupUser", findUsersQueryText);
    using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query)) {
      foreach (ManagementObject foundObject in searcher.Get()) {
        foreach (var property in foundObject.Properties) {
          if (property.Name == "PartComponent") {
            ManagementPath managementPath = new ManagementPath(foundObject["PartComponent"].ToString());
            if (managementPath.ClassName == "Win32_UserAccount") {
              using (ManagementObject userAccount = new ManagementObject(managementPath)) {
                foreach (var x in userAccount.Properties) {
                  //Exception raised here while trying to enumerate properties
                  //System.Management.ManagementException: Not found 
                  //  em System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode)
                  //  em System.Management.ManagementObject.Initialize(Boolean getObject)
                  //  em System.Management.ManagementBaseObject.get_Properties()
                  //  em WmiTest.AccountManager.GetUsersInGroup(String domainName, String groupName)
                  Console.WriteLine(String.Format("  Name: '{0}' Value: '{1}'\r\n", x.Name ?? "null", x.Value ?? "null"));
                }
              }
            }
          }
        }
      }
    }
  }
  </code></pre>
  
  <p>We've tried deleting the WMI repository but the problem remains.</p>
  
  <p>What could be causing these problems with the WMI code?
  How can I get more information about what is going wrong?</p>