tankcr

computerlist class

May 29th, 2014
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.DirectoryServices;
  7.  
  8. namespace GetLogs
  9. {
  10. class GetComputers
  11. {
  12. public static List<string> GetComputerNames()
  13. {
  14. GetComputers ComputerNames = new GetComputers();
  15. string domain = Environment.UserDomainName;
  16. DirectoryEntry entry = new DirectoryEntry("LDAP://"+domain);
  17. DirectorySearcher mySearcher = new DirectorySearcher(entry);
  18. mySearcher.Filter = ("(objectClass=computer)");
  19. mySearcher.SizeLimit = int.MaxValue;
  20. mySearcher.PageSize = int.MaxValue;
  21.  
  22. foreach (SearchResult resEnt in mySearcher.FindAll())
  23. {
  24. //"CN=SGSVG007DC"
  25. string ComputerName = resEnt.GetDirectoryEntry().Name;
  26. if (ComputerName.StartsWith("CN="))
  27. ComputerName = ComputerName.Remove(0, "CN=".Length);
  28. ComputerNames.Add(ComputerName);
  29. }
  30.  
  31. mySearcher.Dispose();
  32. entry.Dispose();
  33.  
  34. return ComputerNames;
  35. }
  36. }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment