开发者

WMI Flakiness / Enumerating User Accounts

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:

  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"]);
  }

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.

  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"]);
  }

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.

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:开发者_StackOverflow中文版 '{0}' Value: '{1}'\r\n", x.Name ?? "null", x.Value ?? "null"));
              }
            }
          }
        }
      }
    }
  }
}

We've tried deleting the WMI repository but the problem remains.

What could be causing these problems with the WMI code? How can I get more information about what is going wrong?


Nearly ALL WMI problems I've run into when it works on one client but not another boil down to the OS and service pack level.

MS has made a LOT of fixes/changes/additions to the WMI objects over the years. We have one client with 17,000 machines that we installed a service that monitors some WMI information on. The machines run the gamut from XP RTM on up to Windows 7.

That was a nightmare. The differences in the level of WMI information available was astounding. For example, just getting the CPU type was hit or miss depending on patch level.

I would say that Step 1 is to get the client machine patched up to the latest service pack available for that OS. Then try again.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜