Advertisement
tankcr

GetComputers

May 30th, 2014
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 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. public class Computers
  11. {
  12. string computerName;
  13.  
  14. public string ComputerName
  15. {
  16. get { return computerName; }
  17. set { computerName = value; }
  18. }
  19.  
  20. public Computers()
  21. {
  22.  
  23. }
  24. }
  25. public class GetComputers
  26. {
  27. public static List<Computers> GetComputerNames(string domain, string systemtype)
  28. {
  29. List<Computers> computers = new List<Computers>();
  30. GetComputers ComputerNames = new GetComputers();
  31. //string domain = Environment.UserDomainName;
  32. DirectoryEntry entry = new DirectoryEntry("LDAP://"+domain);
  33. DirectorySearcher mySearcher = new DirectorySearcher(entry);
  34. mySearcher.Filter = ("(objectClass=computer)");
  35. mySearcher.SizeLimit = int.MaxValue;
  36. mySearcher.PageSize = int.MaxValue;
  37. foreach (SearchResult resEnt in mySearcher.FindAll())
  38. {
  39.  
  40. string type = "";
  41. if (systemtype == "Computers") { type = ("Windows Server"); }
  42. if (systemtype == "Servers") { type != ("Windows Server"); }
  43. string ComputerName = resEnt.GetDirectoryEntry().Name;
  44. if (ComputerName.StartsWith(type))
  45. {
  46. if (ComputerName.StartsWith("CN="))
  47. ComputerName = ComputerName.Remove(0, "CN=".Length);
  48. computers.Add(new Computers() { ComputerName = ComputerName });
  49. }
  50. }
  51.  
  52. mySearcher.Dispose();
  53. entry.Dispose();
  54.  
  55. return computers;
  56. }
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement