WMI 查询以获取已启用的用户帐户。我的 WHERE 子句有什么问题?

WMI Query to get enabled user accounts. What is wrong with my WHERE clause?

我正在尝试获取本地计算机上未禁用用户帐户的列表以及每个用户的 SID 值。我无法使用 DirectoryEntry class 进行这项工作,因为返回的用户在 ObjectSecurity 字段中都为 NULL。所以我尝试使用 ManagementObjectSearcher

SelectQuery sQuery = new SelectQuery("Win32_UserAccount"); );
var searcher = new ManagementObjectSearcher(sQuery);

返回的用户有我需要的信息,但是这个查询给了我禁用的帐户。我想添加一个 WHERE 子句来说明这一点,但我做对了。

我试过了

new SelectQuery("Win32_UserAccount WHERE NOT Disabled");
new SelectQuery("Win32_UserAccount WHERE Disabled='False'");
new SelectQuery("Win32_UserAccount WHERE Disabled=False");

所有这些都会导致异常。谁能告诉我这个怎么表达?

    在我们的案例中,
  • ManagementObjectSearcher 根据指定查询检索管理对象集合 SELECT * FROM Win32_UserAccount

  • userSearcher.Get() : 调用指定的 WMI 查询和 returns 结果集合。


在参考中添加System.Management

static void Main(string[] args)
 {
    ManagementObjectSearcher usersSearcher = new ManagementObjectSearcher(@"SELECT * FROM Win32_UserAccount");
    ManagementObjectCollection users = usersSearcher.Get();

    var localUsers = users.Cast<ManagementObject>().Where(
        u => (bool)u["Disabled"] == false);

    foreach (ManagementObject user in localUsers)
    {
        Console.WriteLine("Account Type: " + user["AccountType"].ToString());               
        Console.WriteLine("Full Name: " + user["FullName"].ToString());
        Console.WriteLine("SID: " + user["SID"].ToString());
        Console.WriteLine("SID Type: " + user["SIDType"].ToString());
        Console.WriteLine("Status: " + user["Status"].ToString());
            Console.WriteLine("Disabled: " + user["Disabled"].ToString());
        Console.WriteLine("Domain: " + user["Domain"].ToString());
    }
 }

 static void Main(string[] args)
 {
    ManagementObjectSearcher usersSearcher = new ManagementObjectSearcher(@"SELECT * FROM Win32_UserAccount WHERE Disabled=False");
    ManagementObjectCollection users = usersSearcher.Get();


    foreach (ManagementObject user in users)
    {
        Console.WriteLine("Account Type: " + user["AccountType"].ToString());               
        Console.WriteLine("Full Name: " + user["FullName"].ToString());
        Console.WriteLine("SID: " + user["SID"].ToString());
        Console.WriteLine("SID Type: " + user["SIDType"].ToString());
        Console.WriteLine("Status: " + user["Status"].ToString());
            Console.WriteLine("Disabled: " + user["Disabled"].ToString());
        Console.WriteLine("Domain: " + user["Domain"].ToString());
    }
 }

您没有使用单独接受查询的 SearchQuery correctly; to specify a condition, you have to either give a full query string, or use the SearchQuery(String, String) 构造函数:

var query = new SelectQuery("SELECT * FROM Win32_UserAccount WHERE Disabled=False");
var searcher = new ManagementObjectSearcher(query);

var query = new SelectQuery("Win32_UserAccount", "Disabled=False");
var searcher = new ManagementObjectSearcher(query);

但是,您可以使用直接接受查询字符串的 ManagementObjectSearcher(String) 构造函数来简化代码:

var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_UserAccount WHERE Disabled=False");