-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdGroupMemberPrinter.cs
79 lines (72 loc) · 2.86 KB
/
AdGroupMemberPrinter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System;
using System.Linq;
using System.DirectoryServices.AccountManagement;
namespace ActiveDirectoryScanner
{
/// <summary>
/// A class that can print the members of an Active Directory group.
/// </summary>
public class AdGroupMemberPrinter : IAdGroupMemberPrinter
{
/// <summary>
/// Prints the members of a specified Active Directory group.
/// </summary>
/// <param name="groupName">The name of the Active Directory whose members should be printed.</param>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="groupName"/> is null.</exception>
/// <exception cref="ArgumentException">Thrown if <paramref name="groupName"/> is empty or whitespace.</exception>
public void PrintGroupMembers(string groupName)
{
// Validate input.
if (groupName == null)
{
throw new ArgumentNullException(nameof(groupName));
}
if (string.IsNullOrWhiteSpace(groupName))
{
throw new ArgumentException($"'{nameof(groupName)}' cannot be empty or whitespace.", nameof(groupName));
}
// Set up a domain context.
try
{
using (var principalContext = new PrincipalContext(ContextType.Domain))
{
// Search for the specified group.
GroupPrincipal group = null;
try
{
group = GroupPrincipal.FindByIdentity(principalContext, groupName);
}
catch (MultipleMatchesException)
{
Console.WriteLine($"Error: Multiple matches found for '{groupName}'.");
return;
}
// If it wasn't found, display an error message.
if (group == null)
{
Console.WriteLine($"Error: Group '{groupName}' not found.");
return;
}
// Else, print the group's members.
var members = group.GetMembers();
if (members.Count() == 0)
{
Console.WriteLine($"Group '{groupName}' contains no members.");
}
else
{
Console.WriteLine($"Members of group '{groupName}':");
foreach (var member in members)
{
Console.WriteLine(member.DisplayName);
}
}
}
}
catch (Exception e)
{
Console.WriteLine($"Error: {e.Message}");
}
}
}
}