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

时间:2014-08-12 10:19:09

标签: 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());
            }
        }
    }
}

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

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

etc.

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

查看我们的活动目录后,我无法确定条目显示的顺序。而且我注意到没有像"计算机" (见图)出现。

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

建议赞赏:)

1 个答案:

答案 0 :(得分: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;
        }
相关问题