Get active directory computers of a specific group using the memberOf attribute using C #

I am trying to get all the names of computers that are in the active group "Standard". The AD tree is as follows:

adtree

I tried to get computers to use the memberOf attribute (I found the attributes on this page: 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, since it did not show any messages, I found out that the "memberOf" attribute returns some strange lines. I used MessageBox.Show(entry.Properties["memberOf"].Value.ToString()); to get the value of the "memberOf" attribute. This is what I get:

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

There are many more MsgBoxes, but each box looks like this.

After searching in our active directory, I could not determine the display order of the entries. And I noticed that nothing like "Computer" (see. Image) does not appear.

Conclusion: I just want to get computers in bbad.lan > Computer > Standard , but the results of my code confuse me, so now I am very puzzled.

The offer is appreciated :)

+6
source share
1 answer

Try using the main computer 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; } 
+2
source

Source: https://habr.com/ru/post/973705/


All Articles