[英]Get active directory computers of specific group using “memberOf” attribute using C#
I'm trying to get all the computer names which are in the active directory "Standard" group. 我正在尝试获取活动目录“标准”组中的所有计算机名称。 The AD tree looks like this: AD树如下所示:
I tried to get the computers using "memberOf" attribute (I found the attributes on this page: http://www.kouti.com/tables/userattributes.htm ). 我尝试使用“ memberOf”属性获取计算机(我在此页面上找到了属性: http : //www.kouti.com/tables/userattributes.htm )。 So I have this code: 所以我有这段代码:
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());
}
}
}
}
After debugging this code because it didn't show any message boxes I found out that the "memberOf" attribute returns some strange strings. 在调试该代码后,因为它没有显示任何消息框,我发现“ memberOf”属性返回了一些奇怪的字符串。 I used MessageBox.Show(entry.Properties["memberOf"].Value.ToString());
我使用了MessageBox.Show(entry.Properties["memberOf"].Value.ToString());
to get the value of "memberOf" attribute. 获取“ memberOf”属性的值。 This is what I get: 这是我得到的:
1. MsgBox: CN=Gäste,CN=Builtin,DC=bbad,DC=lan
2. MsgBox: System.Object[]
etc.
There are much more MsgBoxes but every box is like this. MsgBoxes还有更多,但是每个盒子都是这样。
After looking in our active directory I couldn't figure out the order the entries gets displayed. 在我们的活动目录中查找后,我无法确定条目的显示顺序。 And I noticed that nothing like "Computer" (see the image) shows up. 而且我注意到没有出现“计算机”之类的东西(见图)。
Conclusion: I just want to get the computers in bbad.lan > Computer > Standard
but the results of my code confuse me so I'm quite perplexed now. 结论:我只想在bbad.lan > Computer > Standard
获得计算机,但是代码的结果使我感到困惑,所以现在我很困惑。
Suggestion appreciated :) 建议表示赞赏:)
Try the following using the computer principal class: 使用计算机主体类尝试以下方法:
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.