用户所属的wmi组

发布于 2024-08-28 11:24:23 字数 497 浏览 4 评论 0原文

我有找到所有本地用户的代码:

        ManagementObjectSearcher userSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_UserAccount");
        foreach (ManagementObject user in userSearcher.Get())
        {
            if ((bool)user["LocalAccount"])
            {
                string UserName = (string)user["Name"];
            }
        }
        return;

现在我想要帮助我选择用户所在的所有组的代码。我知道有一个名为 Win32_GroupUser 的表,我必须使用 PartComponent 来指示用户名,但我无法创建查询。请帮忙提供有关 WQL 的信息。

i have the code which find all local users:

        ManagementObjectSearcher userSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_UserAccount");
        foreach (ManagementObject user in userSearcher.Get())
        {
            if ((bool)user["LocalAccount"])
            {
                string UserName = (string)user["Name"];
            }
        }
        return;

now i want code which help me to select all groups in which user consist. I know that there is table called Win32_GroupUser and i must use PartComponent to indicate user name but i cant create query. Please help with information about WQL.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花伊自在美 2024-09-04 11:24:23

查询将类似于:

select * from Win32_GroupUser 
       WHERE PartComponent="Win32_Group.Domain='Workgroup',Name='LocalAccount'" 

string query = "Select * from Win32_GroupUser Where PartComponent=";
query += '"Win32_Group.Domain=';
query += "'" + user["Domain"] + "'";
query += ",Name='" + user["Name"] + "'";
query += '"';

您需要传递域名和用户名来查找关联的组。出于性能原因,我推荐 StringBuilder 类。

我从这篇文章中得出这个答案。

The query will look something like:

select * from Win32_GroupUser 
       WHERE PartComponent="Win32_Group.Domain='Workgroup',Name='LocalAccount'" 

string query = "Select * from Win32_GroupUser Where PartComponent=";
query += '"Win32_Group.Domain=';
query += "'" + user["Domain"] + "'";
query += ",Name='" + user["Name"] + "'";
query += '"';

You need to pass the domain name and username to lookup the associated groups. For performance reasons I would recommend StringBuilder class.

I derived this answer from this article.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文