Groups/GetOktaGroupMember.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
using System.Management.Automation;
using Okta.Core.Models;
 
namespace Okta.Core.Automation
{
    [Cmdlet(VerbsCommon.Get, "OktaGroupMember")]
    public class GetOktaGroupMember : OktaCmdlet
    {
        [Parameter(
            Mandatory = true,
            ValueFromPipelineByPropertyName = true,
            Position = 0,
            HelpMessage = "ID or Name of the group to retrieve"
        )]
        public string IdOrName { get; set; }
 
        protected override void ProcessRecord()
        {
            var groupsClient = Client.GetGroupsClient();
            Models.Group group = null;
            try
            {
                group = groupsClient.Get(IdOrName);
            }
            catch (OktaException)
            {
                group = groupsClient.GetByName(IdOrName);
            }
            if (group != null)
            {
                var groupUsersClient = Client.GetGroupUsersClient(group);
                var users = groupUsersClient.GetFilteredEnumerator();
                WriteObject(users);
            }
            else
            {
                WriteWarning(string.Format("The group with ID or name {0} is invalid. Please try again", IdOrName));
            }
        }
    }
}