使用C#使用"memberOf"属性获取特定组的活动目录计算机

Rom*_*man 6 c# active-directory

我正在尝试获取活动目录"标准"组中的所有计算机名称.AD树看起来像这样:

adtree

我尝试使用"memberOf"属性获取计算机(我在此页面上找到了属性:http://www.kouti.com/tables/userattributes.htm).所以我有这个代码:

using (var context = new PrincipalContext(ContextType.Domain, "bbad.lan"))
{
    using (var searcher = new PrincipalSearcher(new UserPrincipal(context)))
    {
        foreach (var result in searcher.FindAll())
        {
            DirectoryEntry entry = result.GetUnderlyingObject() as DirectoryEntry;

            if (entry.Properties["memberOf"].Value == "Computer")
            {
                MessageBox.Show("aaa: " + entry.Properties["Name"].Value.ToString());
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

调试此代码后,因为它没有显示任何消息框,我发现"memberOf"属性返回一些奇怪的字符串.我曾经MessageBox.Show(entry.Properties["memberOf"].Value.ToString());得到"memberOf"属性的值.这就是我得到的:

1. MsgBox: CN=Gäste,CN=Builtin,DC=bbad,DC=lan
2. MsgBox: System.Object[]

etc.
Run Code Online (Sandbox Code Playgroud)

还有更多的MsgBox,但每个盒子都是这样的.

查看我们的活动目录后,我无法确定条目显示的顺序.而且我注意到没有像"电脑"(见图)那样出现.

结论:我只是想让计算机进入,bbad.lan > Computer > Standard但我的代码结果让我困惑,所以我现在非常困惑.

建议赞赏:)

Bay*_*eni 2

使用计算机主体类尝试以下操作:

        try
        {
            PrincipalContext ctx = new PrincipalContext (ContextType.Domain, "ADDomain", "OU=Standard,OU=Computer,DC=bbad,DC=lan");
            PrincipalSearcher searcher  = new PrincipalSearcher(new ComputerPrincipal(ctx));

            foreach (ComputerPrincipal compPrincipal  in searcher.FindAll())
            {
                //DO your logic
            } 


        }
        catch (Exception ex)
        {
            throw;
        }
Run Code Online (Sandbox Code Playgroud)