public void DoRolesExist(PrincipalContext context, List <string> roles, bool showDetail = false) { Console.ForegroundColor = ConsoleColor.Blue; Console.WriteLine("### Testing if all the roles exist."); List <string> notfound = new List <string>(); foreach (string role in roles) { using (var group = GroupPrincipal.FindByIdentity(context, role)) { if (group == null) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($"[{role}] does not exist"); notfound.Add(role); } else { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine($"[{role}] exists"); if (showDetail) { Print.PrintGroup(group, true, true); } } } } Console.ResetColor(); Console.WriteLine("\n============================================="); }
public static void PrintGroup(GroupPrincipal group, bool showMembers = false, bool showGroups = false) { Console.WriteLine("Name: " + group.Name); Console.WriteLine("DistinguishedName: " + group.DistinguishedName); Console.WriteLine("DisplayName: " + group.DisplayName); Console.WriteLine("SamAccountName: " + group.SamAccountName); Console.WriteLine("UserPrincipalName: " + group.UserPrincipalName); Console.WriteLine("Description: " + group.Description); Console.WriteLine("IsSecurityGroup: " + group.IsSecurityGroup); //Console.WriteLine(": " + group.GroupScope.); Console.WriteLine("Guid: " + group.Guid); Console.WriteLine("Sid: " + group.Sid); if (showMembers) { Console.WriteLine(); Console.WriteLine("Members:"); if (group.Members.Count > 0) { foreach (var user in group.Members.OrderBy(x => x.Name)) { if (user is UserPrincipal) { Print.PrintUser(user as UserPrincipal, ""); } if (user is GroupPrincipal) { Print.PrintGroup(group as GroupPrincipal); } } } else { Console.WriteLine("\tNo Members"); } } if (showGroups) { Console.WriteLine(); Console.WriteLine("Member Of:"); var groups = group.GetGroups().OrderBy(x => x.Name); if (groups.Count() > 0) { foreach (GroupPrincipal g in groups) { Print.PrintGroup(g, false, false); } } else { Console.WriteLine("\tNo Membership"); } } Console.WriteLine("==================================="); }