Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.DirectoryServices;
  6. using System.DirectoryServices.AccountManagement;
  7.  
  8. namespace FindComputersInAD
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14.  
  15. string sDomainName = @"sub.domain.com";//e.g. hk.company.com
  16. DirectoryEntry de = new DirectoryEntry();
  17. de.Path = String.Format("LDAP://{0}", sDomainName);
  18. de.Username = "useraccount";
  19. de.Password = "password";
  20.  
  21. try
  22. {
  23. DirectorySearcher ds = new DirectorySearcher();
  24. ds.Filter = "(&objectCategory=computer)";
  25. SearchResultCollection results = ds.FindAll();
  26.  
  27. foreach (SearchResult result in results)
  28. {
  29. ResultPropertyCollection myResultPropertyCollection = result.Properties;
  30. foreach (string myKey in myResultPropertyCollection.PropertyNames)
  31. {
  32. string propertyString = String.Empty;
  33. string tab = " ";
  34. Console.WriteLine("{0} = ", myKey);
  35. foreach (Object myCollection in myResultPropertyCollection[myKey])
  36. {
  37. Console.WriteLine("{0}{1}", tab, myCollection);
  38. Console.ReadKey();
  39. }
  40. }
  41. }
  42. }
  43. catch (Exception ex)
  44. {
  45. Console.WriteLine(ex.Message);
  46. Console.ReadKey();
  47. }
  48.  
  49. }
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement