Guest User

Untitled

a guest
Jun 30th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Linq;
  4. using System.Text;
  5. using System.DirectoryServices;
  6. using System.DirectoryServices.AccountManagement;
  7.  
  8. namespace RemoveFromDistributionGroups
  9. {
  10. class Program
  11. {
  12. private static string sDomain;
  13. private static string sDefaultOU;
  14. private static string sServiceUser;
  15. private static string sServicePassword;
  16.  
  17. static void Main(string[] args)
  18. {
  19. try
  20. {
  21. Console.Write("Type your Domain (i.e: yourcompany.com) ");
  22. sDomain = Console.ReadLine();
  23.  
  24. Console.Write("Type the OU you want to use: (i.e: OU=yourou,DC=yourcompany,DC=com)");
  25. sDefaultOU = Console.ReadLine();
  26.  
  27. Console.Write(@"Username: (i.e.: YOURDOMAIN\Raymund )");
  28. sServiceUser = Console.ReadLine();
  29.  
  30. Console.Write("Password: ");
  31. sServicePassword = Console.ReadLine();
  32.  
  33.  
  34. foreach (UserPrincipal user in GetAllUsers())
  35. {
  36. Console.WriteLine("Processing User : " + user.Name);
  37. foreach (GroupPrincipal group in GetUserGroups(user))
  38. {
  39. if (group.IsSecurityGroup == false) //Distribution Group
  40. {
  41. group.Members.Remove(user);
  42. group.Save();
  43. }
  44. }
  45. }
  46.  
  47. Console.WriteLine("Done! Press a key to exit");
  48. Console.ReadLine();
  49. }
  50. catch (Exception ex)
  51. {
  52. Console.WriteLine("Error Encountered : " + ex.Message);
  53. Console.WriteLine("Press a key to exit");
  54. Console.ReadLine();
  55. }
  56. }
  57. public static PrincipalContext GetPrincipalContext(string sOU)
  58. {
  59. PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain, sDomain, sOU, ContextOptions.Negotiate, sServiceUser, sServicePassword);
  60. return oPrincipalContext;
  61. }
  62. public static ArrayList GetAllUsers()
  63. {
  64. ArrayList myItems = new ArrayList();
  65. PrincipalSearcher oPrincipalSearcher = new PrincipalSearcher();
  66.  
  67.  
  68. UserPrincipal oUserPrincipal = new UserPrincipal(GetPrincipalContext(sDefaultOU));
  69.  
  70. oUserPrincipal.SamAccountName = "*";
  71. oUserPrincipal.Enabled = true;
  72.  
  73. oPrincipalSearcher.QueryFilter = oUserPrincipal;
  74. ((DirectorySearcher)oPrincipalSearcher.GetUnderlyingSearcher()).PageSize = 5000;
  75.  
  76. PrincipalSearchResult<Principal> oPrincipalSearchResults = oPrincipalSearcher.FindAll();
  77. foreach (Principal oResult in oPrincipalSearchResults)
  78. {
  79. myItems.Add(oResult);
  80. }
  81.  
  82. return myItems;
  83. }
  84. public static ArrayList GetUserGroups(UserPrincipal oUserPrincipal)
  85. {
  86. ArrayList myItems = new ArrayList();
  87.  
  88. PrincipalSearchResult<Principal> oPrincipalSearchResult = oUserPrincipal.GetGroups();
  89.  
  90. foreach (Principal oResult in oPrincipalSearchResult)
  91. {
  92. myItems.Add(oResult);
  93. }
  94. return myItems;
  95.  
  96. }
  97.  
  98. }
  99. }
Add Comment
Please, Sign In to add comment