public override void ExecuteCmdlet()
        {
            ADObjectFilterOptions options = new ADObjectFilterOptions
            {
                SPN = ServicePrincipalName,
                Id = ObjectId
            };

            ExecutionBlock(() =>
            {
                // At max 1 SP can be returned with SPN and Id options
                var sp = ActiveDirectoryClient.FilterServicePrincipals(options).FirstOrDefault();

                if (sp == null)
                {
                    throw new InvalidOperationException("ServicePrincipal does not exist.");
                }

                // Get AppObjectId
                string applicationObjectId = ActiveDirectoryClient.GetObjectIdFromApplicationId(sp.ApplicationId.ToString());

                if (!string.IsNullOrEmpty(DisplayName))
                {
                    ApplicationUpdateParameters parameters = new ApplicationUpdateParameters()
                    {
                        DisplayName = DisplayName
                    };

                    if (ShouldProcess(target: sp.Id.ToString(), action: string.Format("Updating properties on application associated with a service principal with object id '{0}'", sp.Id)))
                    {
                        ActiveDirectoryClient.UpdateApplication(applicationObjectId, parameters);
                    }
                }
            });
        }
        public override void ExecuteCmdlet()
        {
            ADObjectFilterOptions options = new ADObjectFilterOptions
            {
                Id = GroupObjectId == Guid.Empty ? null : GroupObjectId.ToString(),
                Paging = true
            };

            do
            {
                WriteObject(ActiveDirectoryClient.GetGroupMembers(options), true);
            } while (!string.IsNullOrEmpty(options.NextLink));
        }
        protected override void ProcessRecord()
        {
            ADObjectFilterOptions options = new ADObjectFilterOptions
            {
                SearchString = SearchString,
                Id = ObjectId == Guid.Empty ? null : ObjectId.ToString(),
                Paging = true
            };

            do
            {
                WriteObject(ActiveDirectoryClient.FilterGroups(options), true);
            } while (!string.IsNullOrEmpty(options.NextLink));
        }
        public override void ExecuteCmdlet()
        {
            ADObjectFilterOptions options = new ADObjectFilterOptions
            {
                SearchString = SearchString,
                SPN = ServicePrincipalName,
                Id = ObjectId == Guid.Empty ? null : ObjectId.ToString(),
                Paging = true
            };

            do
            {
                WriteObject(ActiveDirectoryClient.FilterServicePrincipals(options), true);

            } while (!string.IsNullOrEmpty(options.NextLink));
        }
        public override void ExecuteCmdlet()
        {
            ExecutionBlock(() =>
            {
                ADObjectFilterOptions options = new ADObjectFilterOptions
                {
                    SearchString = SearchString,
                    Id = ObjectId == Guid.Empty ? null : ObjectId.ToString(),
                    Paging = true
                };

                do
                {
                    WriteObject(ActiveDirectoryClient.FilterGroups(options), true);
                } while (!string.IsNullOrEmpty(options.NextLink));

            });
        }
        public override void ExecuteCmdlet()
        {
            ADObjectFilterOptions options = new ADObjectFilterOptions
            {
                SearchString = SearchString,
                UPN = UserPrincipalName,
                Id = ObjectId == Guid.Empty ? null : ObjectId.ToString(),
                Paging = true,
                Mail = Mail
            };

            ExecutionBlock(() =>
            {
                do
                {
                    WriteObject(ActiveDirectoryClient.FilterUsers(options), true);

                } while (!string.IsNullOrEmpty(options.NextLink));
            });
        }
        public override void ExecuteCmdlet()
        {
            ExecutionBlock(() =>
            {
                ADObjectFilterOptions options = new ADObjectFilterOptions
                {
                    Id = GroupObjectId == Guid.Empty ? null : GroupObjectId.ToString(),
                    Paging = true
                };

                PSADObject group = ActiveDirectoryClient.FilterGroups(options).FirstOrDefault();
                if (group == null)
                {
                    throw new KeyNotFoundException(string.Format(ProjectResources.GroupDoesntExist, GroupObjectId));
                }

                do
                {
                    WriteObject(ActiveDirectoryClient.GetGroupMembers(options), true);
                } while (!string.IsNullOrEmpty(options.NextLink));
            });
        }
        public PSADObject GetADObject(ADObjectFilterOptions options)
        {
            PSADObject result = null;

            Debug.Assert(options != null);

            if (IsSet(options.Mail, options.UPN, options.Id))
            {
                result = FilterUsers(options).FirstOrDefault();
            }

            if (result == null && IsSet(options.SPN, options.Id))
            {
                result = FilterServicePrincipals(options).FirstOrDefault();
            }

            if (result == null && IsSet(options.Mail, options.Id))
            {
                result = FilterGroups(options).FirstOrDefault();
            }

            return(result);
        }
        public PSADObject GetADObject(ADObjectFilterOptions options)
        {
            PSADObject result = null;

            Debug.Assert(options != null);

            if (IsSet(options.SignInName, options.Mail, options.UPN, options.Id))
            {
                result = FilterUsers(options).FirstOrDefault();
            }

            if (result == null && IsSet(options.SPN, options.Id))
            {
                result = FilterServicePrincipals(options).FirstOrDefault();
            }

            if (result == null && IsSet(options.Mail, options.Id))
            {
                result = FilterGroups(options).FirstOrDefault();
            }

            return result;
        }
        public Guid GetObjectId(ADObjectFilterOptions options)
        {
            Guid principalId;

            if (options != null && options.Id != null &&
                Guid.TryParse(options.Id, out principalId))
            {
                // do nothing, we have parsed the guid
            }
            else
            {
                PSADObject adObj = GetADObject(options);

                if (adObj == null)
                {
                    throw new KeyNotFoundException("The provided information does not map to an AD object id.");
                }

                principalId = adObj.Id;
            }

            return(principalId);
        }
        public List<PSADUser> FilterUsers(ADObjectFilterOptions options)
        {
            List<PSADUser> users = new List<PSADUser>();
            UserListResult result = new UserListResult();
            User user = null;

            if (!string.IsNullOrEmpty(options.Id) || !string.IsNullOrEmpty(options.UPN))
            {
                try
                {
                    user = GraphClient.User.Get(Normalize(options.Id) ?? Normalize(options.UPN)).User;
                }
                catch {  /* The user does not exist, ignore the exception. */ }

                if (user != null)
                {
                    users.Add(user.ToPSADUser());
                }
            }
            else if (!string.IsNullOrEmpty(options.Mail) || !string.IsNullOrEmpty(options.SignInName))
            {
                try
                {
                    user = GraphClient.User.GetBySignInName(Normalize(options.Mail) ?? Normalize(options.SignInName)).Users.FirstOrDefault();
                }
                catch {  /* The user does not exist, ignore the exception. */ }

                if (user != null)
                {
                    users.Add(user.ToPSADUser());
                }
            }
            else
            {
                if (options.Paging)
                {
                    if (string.IsNullOrEmpty(options.NextLink))
                    {
                        result = GraphClient.User.List(null, options.SearchString);
                    }
                    else
                    {
                        result = GraphClient.User.ListNext(options.NextLink);
                    }

                    users.AddRange(result.Users.Select(u => u.ToPSADUser()));
                    options.NextLink = result.NextLink;
                }
                else
                {
                    result = GraphClient.User.List(null, options.SearchString);
                    users.AddRange(result.Users.Select(u => u.ToPSADUser()));

                    while (!string.IsNullOrEmpty(result.NextLink))
                    {
                        result = GraphClient.User.ListNext(result.NextLink);
                        users.AddRange(result.Users.Select(u => u.ToPSADUser()));
                    }
                }
            }

            return users;
        }
Example #12
0
        public List <PSADGroup> FilterGroups(ADObjectFilterOptions options)
        {
            List <PSADGroup> groups = new List <PSADGroup>();

            if (!string.IsNullOrEmpty(options.Id))
            {
                try
                {
                    // use GetObjectsByObjectId to handle Redirects in the CSP scenario
                    PSADGroup group = this.GetObjectsByObjectId(new List <string> {
                        options.Id
                    }).FirstOrDefault() as PSADGroup;
                    if (group != null)
                    {
                        groups.Add(group);
                    }
                }
                catch { /* The group does not exist, ignore the exception */ }
            }
            else
            {
                Rest.Azure.IPage <ADGroup>            result     = null;
                Rest.Azure.OData.ODataQuery <ADGroup> odataQuery = null;

                if (options.Paging)
                {
                    if (string.IsNullOrEmpty(options.NextLink))
                    {
                        if (options.Mail != null)
                        {
                            odataQuery = new Rest.Azure.OData.ODataQuery <ADGroup>(g => g.Mail == options.Mail);
                        }
                        else
                        {
                            odataQuery = new Rest.Azure.OData.ODataQuery <ADGroup>(g => g.DisplayName.StartsWith(options.SearchString));
                        }

                        result = GraphClient.Groups.List(odataQuery);
                    }
                    else
                    {
                        result = GraphClient.Groups.ListNext(options.NextLink);
                    }

                    groups.AddRange(result.Select(g => g.ToPSADGroup()));
                    options.NextLink = result.NextPageLink;
                }
                else
                {
                    if (options.Mail != null)
                    {
                        odataQuery = new Rest.Azure.OData.ODataQuery <ADGroup>(g => g.Mail == options.Mail);
                    }
                    else
                    {
                        odataQuery = new Rest.Azure.OData.ODataQuery <ADGroup>(g => g.DisplayName.StartsWith(options.SearchString));
                    }

                    result = GraphClient.Groups.List(odataQuery);
                    groups.AddRange(result.Select(g => g.ToPSADGroup()));

                    while (!string.IsNullOrEmpty(result.NextPageLink))
                    {
                        result = GraphClient.Groups.ListNext(result.NextPageLink);
                        groups.AddRange(result.Select(g => g.ToPSADGroup()));
                    }
                }
            }

            return(groups);
        }
        public List <PSADServicePrincipal> FilterServicePrincipals(ADObjectFilterOptions options)
        {
            List <PSADServicePrincipal> servicePrincipals = new List <PSADServicePrincipal>();
            ServicePrincipalListResult  result            = new ServicePrincipalListResult();
            ServicePrincipal            servicePrincipal  = null;

            if (!string.IsNullOrEmpty(options.Id))
            {
                try
                {
                    servicePrincipal = GraphClient.ServicePrincipal.Get(options.Id).ServicePrincipal;
                }
                catch { /* The user does not exist, ignore the exception. */ }

                if (servicePrincipal != null)
                {
                    servicePrincipals.Add(servicePrincipal.ToPSADServicePrincipal());
                }
            }
            else if (!string.IsNullOrEmpty(options.SPN))
            {
                try
                {
                    servicePrincipal = GraphClient.ServicePrincipal.GetByServicePrincipalName(options.SPN).ServicePrincipals.FirstOrDefault();
                }
                catch { /* The user does not exist, ignore the exception. */ }

                if (servicePrincipal != null)
                {
                    servicePrincipals.Add(servicePrincipal.ToPSADServicePrincipal());
                }
            }
            else
            {
                if (options.Paging)
                {
                    if (string.IsNullOrEmpty(options.NextLink))
                    {
                        result = GraphClient.ServicePrincipal.List(options.SearchString);
                    }
                    else
                    {
                        result = GraphClient.ServicePrincipal.ListNext(options.NextLink);
                    }

                    servicePrincipals.AddRange(result.ServicePrincipals.Select(u => u.ToPSADServicePrincipal()));
                    options.NextLink = result.NextLink;
                }
                else
                {
                    result = GraphClient.ServicePrincipal.List(options.SearchString);
                    servicePrincipals.AddRange(result.ServicePrincipals.Select(u => u.ToPSADServicePrincipal()));

                    while (!string.IsNullOrEmpty(result.NextLink))
                    {
                        result = GraphClient.ServicePrincipal.ListNext(result.NextLink);
                        servicePrincipals.AddRange(result.ServicePrincipals.Select(u => u.ToPSADServicePrincipal()));
                    }
                }
            }

            return(servicePrincipals);
        }
        public List <PSADUser> FilterUsers(ADObjectFilterOptions options)
        {
            List <PSADUser> users  = new List <PSADUser>();
            UserListResult  result = new UserListResult();
            User            user   = null;

            if (!string.IsNullOrEmpty(options.Id) || !string.IsNullOrEmpty(options.UPN))
            {
                try
                {
                    user = GraphClient.User.Get(Normalize(options.Id) ?? Normalize(options.UPN)).User;
                }
                catch { /* The user does not exist, ignore the exception. */ }

                if (user != null)
                {
                    users.Add(user.ToPSADUser());
                }
            }
            else if (!string.IsNullOrEmpty(options.Mail) || !string.IsNullOrEmpty(options.SignInName))
            {
                try
                {
                    user = GraphClient.User.GetBySignInName(Normalize(options.Mail) ?? Normalize(options.SignInName)).Users.FirstOrDefault();
                }
                catch { /* The user does not exist, ignore the exception. */ }

                if (user != null)
                {
                    users.Add(user.ToPSADUser());
                }
            }
            else
            {
                if (options.Paging)
                {
                    if (string.IsNullOrEmpty(options.NextLink))
                    {
                        result = GraphClient.User.List(null, options.SearchString);
                    }
                    else
                    {
                        result = GraphClient.User.ListNext(options.NextLink);
                    }

                    users.AddRange(result.Users.Select(u => u.ToPSADUser()));
                    options.NextLink = result.NextLink;
                }
                else
                {
                    result = GraphClient.User.List(null, options.SearchString);
                    users.AddRange(result.Users.Select(u => u.ToPSADUser()));

                    while (!string.IsNullOrEmpty(result.NextLink))
                    {
                        result = GraphClient.User.ListNext(result.NextLink);
                        users.AddRange(result.Users.Select(u => u.ToPSADUser()));
                    }
                }
            }

            return(users);
        }
Example #15
0
        public List <PSADUser> FilterUsers(ADObjectFilterOptions options)
        {
            List <PSADUser> users = new List <PSADUser>();

            Rest.Azure.IPage <User> result = null;
            User user = null;

            if (!string.IsNullOrEmpty(options.Id))
            {
                try
                {
                    user = GraphClient.Users.Get(Normalize(options.Id));
                }
                catch { /* The user does not exist, ignore the exception. */ }

                if (user != null)
                {
                    users.Add(user.ToPSADUser());
                }
            }
            else if (!string.IsNullOrEmpty(options.UPN) || !string.IsNullOrEmpty(options.Mail))
            {
                try
                {
                    string upnOrMail = Normalize(options.UPN) ?? Normalize(options.Mail);
                    result = GraphClient.Users.List(new Rest.Azure.OData.ODataQuery <User>(u => u.UserPrincipalName == upnOrMail));
                }
                catch { /* The user does not exist, ignore the exception. */ }

                if (result != null)
                {
                    users.AddRange(result.Select(u => u.ToPSADUser()));
                }
            }
            else
            {
                if (options.Paging)
                {
                    if (string.IsNullOrEmpty(options.NextLink))
                    {
                        result = GraphClient.Users.List(new Rest.Azure.OData.ODataQuery <User>(u => u.DisplayName.StartsWith(options.SearchString)));
                    }
                    else
                    {
                        result = GraphClient.Users.ListNext(options.NextLink);
                    }

                    users.AddRange(result.Select(u => u.ToPSADUser()));
                    options.NextLink = result.NextPageLink;
                }
                else
                {
                    result = GraphClient.Users.List(new Rest.Azure.OData.ODataQuery <User>(u => u.DisplayName.StartsWith(options.SearchString)));
                    users.AddRange(result.Select(u => u.ToPSADUser()));

                    while (!string.IsNullOrEmpty(result.NextPageLink))
                    {
                        result = GraphClient.Users.ListNext(result.NextPageLink);
                        users.AddRange(result.Select(u => u.ToPSADUser()));
                    }
                }
            }

            return(users);
        }
        protected Guid GetObjectId(Guid objectId, string upn, string spn)
        {
            var filter = new ADObjectFilterOptions()
                {
                    Id = (objectId != null && objectId != Guid.Empty) ? objectId.ToString() : null,
                    UPN = upn,
                    SPN = spn,                    
                    Paging = true,
                };

            var obj = ActiveDirectoryClient.GetADObject(filter);

            if (obj == null && !string.IsNullOrWhiteSpace(upn))
            {
                filter = new ADObjectFilterOptions()
                {
                    Mail = upn,
                    Paging = true,
                };
                obj = ActiveDirectoryClient.GetADObject(filter);
            }

            if (obj != null)
                return obj.Id;
            else
                throw new ArgumentException(string.Format(PSKeyVaultProperties.Resources.ADObjectNotFound, filter.ActiveFilter, ActiveDirectoryClient.GraphClient.TenantID));
        }
        public Guid GetObjectId(ADObjectFilterOptions options)
        {
            Guid principalId;
            if (options != null && options.Id != null
                && Guid.TryParse(options.Id, out principalId))
            {
                // do nothing, we have parsed the guid
            }
            else
            {
                PSADObject adObj = GetADObject(options);

                if (adObj == null)
                {
                    throw new KeyNotFoundException("The provided information does not map to an AD object id.");
                }

                principalId = adObj.Id;
            }

            return principalId;
        }
        public List<PSADGroup> FilterGroups(ADObjectFilterOptions options)
        {
            List<PSADGroup> groups = new List<PSADGroup>();
            Group group = null;

            if (!string.IsNullOrEmpty(options.Id))
            {
                try
                {
                    group = GraphClient.Group.Get(options.Id).Group;
                }
                catch {  /* The group does not exist, ignore the exception */ }

                if (group != null)
                {
                    groups.Add(group.ToPSADGroup());
                }
            }
            else
            {
                GroupListResult result = new GroupListResult();

                if (options.Paging)
                {
                    if (string.IsNullOrEmpty(options.NextLink))
                    {
                        result = GraphClient.Group.List(options.Mail, options.SearchString);
                    }
                    else
                    {
                        result = GraphClient.Group.ListNext(options.NextLink);
                    }

                    groups.AddRange(result.Groups.Select(g => g.ToPSADGroup()));
                    options.NextLink = result.NextLink;
                }
                else
                {
                    result = GraphClient.Group.List(options.Mail, options.SearchString);
                    groups.AddRange(result.Groups.Select(g => g.ToPSADGroup()));

                    while (!string.IsNullOrEmpty(result.NextLink))
                    {
                        result = GraphClient.Group.ListNext(result.NextLink);
                        groups.AddRange(result.Groups.Select(g => g.ToPSADGroup()));
                    }
                }
            }

            return groups;
        }
Example #19
0
        public List <PSADServicePrincipal> FilterServicePrincipals(ADObjectFilterOptions options)
        {
            List <PSADServicePrincipal> servicePrincipals = new List <PSADServicePrincipal>();

            Rest.Azure.IPage <ServicePrincipal> result = null;
            ServicePrincipal servicePrincipal          = null;

            if (!string.IsNullOrEmpty(options.Id))
            {
                try
                {
                    servicePrincipal = GraphClient.ServicePrincipals.Get(options.Id);
                }
                catch { /* The user does not exist, ignore the exception. */ }

                if (servicePrincipal != null)
                {
                    servicePrincipals.Add(servicePrincipal.ToPSADServicePrincipal());
                }
            }
            else if (!string.IsNullOrEmpty(options.SPN))
            {
                try
                {
                    servicePrincipal = GraphClient.ServicePrincipals.List(new Rest.Azure.OData.ODataQuery <ServicePrincipal>(s => s.ServicePrincipalNames.Contains(options.SPN))).FirstOrDefault();
                }
                catch { /* The user does not exist, ignore the exception. */ }

                if (servicePrincipal != null)
                {
                    servicePrincipals.Add(servicePrincipal.ToPSADServicePrincipal());
                }
            }
            else
            {
                if (options.Paging)
                {
                    if (string.IsNullOrEmpty(options.NextLink))
                    {
                        result = GraphClient.ServicePrincipals.List(new Rest.Azure.OData.ODataQuery <ServicePrincipal>(s => s.DisplayName.StartsWith(options.SearchString)));
                    }
                    else
                    {
                        result = GraphClient.ServicePrincipals.ListNext(options.NextLink);
                    }

                    servicePrincipals.AddRange(result.Select(u => u.ToPSADServicePrincipal()));
                    options.NextLink = result.NextPageLink;
                }
                else
                {
                    result = GraphClient.ServicePrincipals.List(new Rest.Azure.OData.ODataQuery <ServicePrincipal>(s => s.DisplayName.StartsWith(options.SearchString)));
                    servicePrincipals.AddRange(result.Select(u => u.ToPSADServicePrincipal()));

                    while (!string.IsNullOrEmpty(result.NextPageLink))
                    {
                        result = GraphClient.ServicePrincipals.ListNext(result.NextPageLink);
                        servicePrincipals.AddRange(result.Select(u => u.ToPSADServicePrincipal()));
                    }
                }
            }

            return(servicePrincipals);
        }
        public List<PSADGroup> FilterGroups(ADObjectFilterOptions options)
        {
            List<PSADGroup> groups = new List<PSADGroup>();
            ADGroup group = null;

            if (!string.IsNullOrEmpty(options.Id))
            {
                try
                {
                    group = GraphClient.Groups.Get(options.Id);
                }
                catch {  /* The group does not exist, ignore the exception */ }

                if (group != null)
                {
                    groups.Add(group.ToPSADGroup());
                }
            }
            else
            {
                Rest.Azure.IPage<ADGroup> result = null;
                Rest.Azure.OData.ODataQuery<ADGroup> odataQuery = null;

                if (options.Paging)
                {
                    if (string.IsNullOrEmpty(options.NextLink))
                    {
                        if (options.Mail != null)
                        {
                            odataQuery = new Rest.Azure.OData.ODataQuery<ADGroup>(g => g.Mail == options.Mail);
                        }
                        else
                        {
                            odataQuery = new Rest.Azure.OData.ODataQuery<ADGroup>(g => g.DisplayName.StartsWith(options.SearchString));
                        }

                        result = GraphClient.Groups.List(odataQuery);
                    }
                    else
                    {
                        result = GraphClient.Groups.ListNext(options.NextLink);
                    }

                    groups.AddRange(result.Select(g => g.ToPSADGroup()));
                    options.NextLink = result.NextPageLink;
                }
                else
                {

                    if (options.Mail != null)
                    {
                        odataQuery = new Rest.Azure.OData.ODataQuery<ADGroup>(g => g.Mail == options.Mail);
                    }
                    else
                    {
                        odataQuery = new Rest.Azure.OData.ODataQuery<ADGroup>(g => g.DisplayName.StartsWith(options.SearchString));
                    }

                    result = GraphClient.Groups.List(odataQuery);
                    groups.AddRange(result.Select(g => g.ToPSADGroup()));

                    while (!string.IsNullOrEmpty(result.NextPageLink))
                    {
                        result = GraphClient.Groups.ListNext(result.NextPageLink);
                        groups.AddRange(result.Select(g => g.ToPSADGroup()));
                    }
                }
            }

            return groups;
        }
        public List<PSADObject> GetGroupMembers(ADObjectFilterOptions options)
        {
            List<PSADObject> members = new List<PSADObject>();
            PSADObject group = FilterGroups(options).FirstOrDefault();

            if (group != null)
            {
                GetObjectsResult result = new GetObjectsResult();

                if (options.Paging)
                {
                    if (string.IsNullOrEmpty(options.NextLink))
                    {
                        result = GraphClient.Group.GetGroupMembers(group.Id.ToString());
                    }
                    else
                    {
                        result = GraphClient.Group.GetGroupMembersNext(result.NextLink);
                    }

                    members.AddRange(result.AADObject.Select(u => u.ToPSADObject()));
                    options.NextLink = result.NextLink;
                }
                else
                {
                    result = GraphClient.Group.GetGroupMembers(group.Id.ToString());
                    members.AddRange(result.AADObject.Select(u => u.ToPSADObject()));

                    while (!string.IsNullOrEmpty(result.NextLink))
                    {
                        result = GraphClient.Group.GetGroupMembersNext(result.NextLink);
                        members.AddRange(result.AADObject.Select(u => u.ToPSADObject()));
                    }
                }
            }

            return members;
        }
        public List<PSADUser> FilterUsers(ADObjectFilterOptions options)
        {
            List<PSADUser> users = new List<PSADUser>();
            Rest.Azure.IPage<User> result = null;
            User user = null;

            if (!string.IsNullOrEmpty(options.Id))
            {
                try
                {
                    user = GraphClient.Users.Get(Normalize(options.Id));
                }
                catch {  /* The user does not exist, ignore the exception. */ }

                if (user != null)
                {
                    users.Add(user.ToPSADUser());
                }
            }
            else if (!string.IsNullOrEmpty(options.UPN) || !string.IsNullOrEmpty(options.Mail))
            {
                try
                {
                    string upnOrMail = Normalize(options.UPN) ?? Normalize(options.Mail);
                    result = GraphClient.Users.List(new Rest.Azure.OData.ODataQuery<User>(u => u.UserPrincipalName == upnOrMail));
                }
                catch {  /* The user does not exist, ignore the exception. */ }

                if (result != null)
                {
                    users.AddRange(result.Select(u => u.ToPSADUser()));
                }
            }
            else
            {
                if (options.Paging)
                {
                    if (string.IsNullOrEmpty(options.NextLink))
                    {
                        result = GraphClient.Users.List(new Rest.Azure.OData.ODataQuery<User>(u => u.DisplayName.StartsWith(options.SearchString)));
                    }
                    else
                    {
                        result = GraphClient.Users.ListNext(options.NextLink);
                    }

                    users.AddRange(result.Select(u => u.ToPSADUser()));
                    options.NextLink = result.NextPageLink;
                }
                else
                {
                    result = GraphClient.Users.List(new Rest.Azure.OData.ODataQuery<User>(u => u.DisplayName.StartsWith(options.SearchString)));
                    users.AddRange(result.Select(u => u.ToPSADUser()));

                    while (!string.IsNullOrEmpty(result.NextPageLink))
                    {
                        result = GraphClient.Users.ListNext(result.NextPageLink);
                        users.AddRange(result.Select(u => u.ToPSADUser()));
                    }
                }

            }

            return users;
        }
        public PSADServicePrincipal RemoveServicePrincipal(string objectId)
        {
            ADObjectFilterOptions options = new ADObjectFilterOptions
            {
                Id = objectId.ToString()
            };

            PSADServicePrincipal servicePrincipal = FilterServicePrincipals(options).FirstOrDefault();
            if (servicePrincipal != null)
            {
                GraphClient.ServicePrincipal.Delete(objectId);
            }
            else
            {
                throw new KeyNotFoundException(string.Format(ProjectResources.ServicePrincipalDoesntExist, objectId));
            }

            return servicePrincipal;
        }
        public List<PSADGroup> FilterGroups(ADObjectFilterOptions options)
        {
            List<PSADGroup> groups = new List<PSADGroup>();

            if (!string.IsNullOrEmpty(options.Id))
            {
                try
                {
                    // use GetObjectsByObjectId to handle Redirects in the CSP scenario
                    PSADGroup group = this.GetObjectsByObjectId(new List<string> { options.Id }).FirstOrDefault() as PSADGroup;
                    if (group != null)
                    {
                        groups.Add(group);
                    }
                }
                catch {  /* The group does not exist, ignore the exception */ }
            }
            else
            {
                Rest.Azure.IPage<ADGroup> result = null;
                Rest.Azure.OData.ODataQuery<ADGroup> odataQuery = null;

                if (options.Paging)
                {
                    if (string.IsNullOrEmpty(options.NextLink))
                    {
                        if (options.Mail != null)
                        {
                            odataQuery = new Rest.Azure.OData.ODataQuery<ADGroup>(g => g.Mail == options.Mail);
                        }
                        else
                        {
                            odataQuery = new Rest.Azure.OData.ODataQuery<ADGroup>(g => g.DisplayName.StartsWith(options.SearchString));
                        }

                        result = GraphClient.Groups.List(odataQuery);
                    }
                    else
                    {
                        result = GraphClient.Groups.ListNext(options.NextLink);
                    }

                    groups.AddRange(result.Select(g => g.ToPSADGroup()));
                    options.NextLink = result.NextPageLink;
                }
                else
                {

                    if (options.Mail != null)
                    {
                        odataQuery = new Rest.Azure.OData.ODataQuery<ADGroup>(g => g.Mail == options.Mail);
                    }
                    else
                    {
                        odataQuery = new Rest.Azure.OData.ODataQuery<ADGroup>(g => g.DisplayName.StartsWith(options.SearchString));
                    }

                    result = GraphClient.Groups.List(odataQuery);
                    groups.AddRange(result.Select(g => g.ToPSADGroup()));

                    while (!string.IsNullOrEmpty(result.NextPageLink))
                    {
                        result = GraphClient.Groups.ListNext(result.NextPageLink);
                        groups.AddRange(result.Select(g => g.ToPSADGroup()));
                    }
                }
            }

            return groups;
        }
        public List<PSADServicePrincipal> FilterServicePrincipals(ADObjectFilterOptions options)
        {
            List<PSADServicePrincipal> servicePrincipals = new List<PSADServicePrincipal>();
            ServicePrincipalListResult result = new ServicePrincipalListResult();
            ServicePrincipal servicePrincipal = null;

            if (!string.IsNullOrEmpty(options.Id))
            {
                try
                {
                    servicePrincipal = GraphClient.ServicePrincipal.Get(options.Id).ServicePrincipal;
                }
                catch {  /* The user does not exist, ignore the exception. */ }

                if (servicePrincipal != null)
                {
                    servicePrincipals.Add(servicePrincipal.ToPSADServicePrincipal());
                }
            }
            else if (!string.IsNullOrEmpty(options.SPN))
            {
                try
                {
                    servicePrincipal = GraphClient.ServicePrincipal.GetByServicePrincipalName(options.SPN).ServicePrincipals.FirstOrDefault();
                }
                catch {  /* The user does not exist, ignore the exception. */ }

                if (servicePrincipal != null)
                {
                    servicePrincipals.Add(servicePrincipal.ToPSADServicePrincipal());
                }
            }
            else
            {
                if (options.Paging)
                {
                    if (string.IsNullOrEmpty(options.NextLink))
                    {
                        result = GraphClient.ServicePrincipal.List(options.SearchString);
                    }
                    else
                    {
                        result = GraphClient.ServicePrincipal.ListNext(options.NextLink);
                    }

                    servicePrincipals.AddRange(result.ServicePrincipals.Select(u => u.ToPSADServicePrincipal()));
                    options.NextLink = result.NextLink;
                }
                else
                {
                    result = GraphClient.ServicePrincipal.List(options.SearchString);
                    servicePrincipals.AddRange(result.ServicePrincipals.Select(u => u.ToPSADServicePrincipal()));

                    while (!string.IsNullOrEmpty(result.NextLink))
                    {
                        result = GraphClient.ServicePrincipal.ListNext(result.NextLink);
                        servicePrincipals.AddRange(result.ServicePrincipals.Select(u => u.ToPSADServicePrincipal()));
                    }
                }
            }

            return servicePrincipals;
        }
Example #26
0
        public List <PSADGroup> FilterGroups(ADObjectFilterOptions options)
        {
            List <PSADGroup> groups = new List <PSADGroup>();
            ADGroup          group  = null;

            if (!string.IsNullOrEmpty(options.Id))
            {
                try
                {
                    group = GraphClient.Groups.Get(options.Id);
                }
                catch { /* The group does not exist, ignore the exception */ }

                if (group != null)
                {
                    groups.Add(group.ToPSADGroup());
                }
            }
            else
            {
                Rest.Azure.IPage <ADGroup>            result     = null;
                Rest.Azure.OData.ODataQuery <ADGroup> odataQuery = null;

                if (options.Paging)
                {
                    if (string.IsNullOrEmpty(options.NextLink))
                    {
                        if (options.Mail != null)
                        {
                            odataQuery = new Rest.Azure.OData.ODataQuery <ADGroup>(g => g.Mail == options.Mail);
                        }
                        else
                        {
                            odataQuery = new Rest.Azure.OData.ODataQuery <ADGroup>(g => g.DisplayName.StartsWith(options.SearchString));
                        }

                        result = GraphClient.Groups.List(odataQuery);
                    }
                    else
                    {
                        result = GraphClient.Groups.ListNext(options.NextLink);
                    }

                    groups.AddRange(result.Select(g => g.ToPSADGroup()));
                    options.NextLink = result.NextPageLink;
                }
                else
                {
                    if (options.Mail != null)
                    {
                        odataQuery = new Rest.Azure.OData.ODataQuery <ADGroup>(g => g.Mail == options.Mail);
                    }
                    else
                    {
                        odataQuery = new Rest.Azure.OData.ODataQuery <ADGroup>(g => g.DisplayName.StartsWith(options.SearchString));
                    }

                    result = GraphClient.Groups.List(odataQuery);
                    groups.AddRange(result.Select(g => g.ToPSADGroup()));

                    while (!string.IsNullOrEmpty(result.NextPageLink))
                    {
                        result = GraphClient.Groups.ListNext(result.NextPageLink);
                        groups.AddRange(result.Select(g => g.ToPSADGroup()));
                    }
                }
            }

            return(groups);
        }
        public List<PSADObject> GetGroupMembers(ADObjectFilterOptions options)
        {
            List<PSADObject> members = new List<PSADObject>();
            Rest.Azure.IPage<AADObject> result = null;

            if (options.Paging)
            {
                if (string.IsNullOrEmpty(options.NextLink))
                {
                    result = GraphClient.Groups.GetGroupMembers(options.Id);
                }
                else
                {
                    result = GraphClient.Groups.GetGroupMembersNext(options.NextLink);
                }

                members.AddRange(result.Select(u => u.ToPSADObject()));
                options.NextLink = result.NextPageLink;
            }
            else
            {
                result = GraphClient.Groups.GetGroupMembers(options.Id);
                members.AddRange(result.Select(u => u.ToPSADObject()));

                while (!string.IsNullOrEmpty(result.NextPageLink))
                {
                    result = GraphClient.Groups.GetGroupMembersNext(result.NextPageLink);
                    members.AddRange(result.Select(u => u.ToPSADObject()));
                }
            }

            return members;
        }
        public List<PSADServicePrincipal> FilterServicePrincipals(ADObjectFilterOptions options)
        {
            List<PSADServicePrincipal> servicePrincipals = new List<PSADServicePrincipal>();
            Rest.Azure.IPage<ServicePrincipal> result = null;
            ServicePrincipal servicePrincipal = null;

            if (!string.IsNullOrEmpty(options.Id))
            {
                try
                {
                    servicePrincipal = GraphClient.ServicePrincipals.Get(options.Id);
                }
                catch {  /* The user does not exist, ignore the exception. */ }

                if (servicePrincipal != null)
                {
                    servicePrincipals.Add(servicePrincipal.ToPSADServicePrincipal());
                }
            }
            else if (!string.IsNullOrEmpty(options.SPN))
            {
                try
                {
                    servicePrincipal = GraphClient.ServicePrincipals.List(new Rest.Azure.OData.ODataQuery<ServicePrincipal>(s => s.ServicePrincipalNames.Contains(options.SPN))).FirstOrDefault();
                }
                catch {  /* The user does not exist, ignore the exception. */ }

                if (servicePrincipal != null)
                {
                    servicePrincipals.Add(servicePrincipal.ToPSADServicePrincipal());
                }
            }
            else
            {

                if (options.Paging)
                {
                    if (string.IsNullOrEmpty(options.NextLink))
                    {
                        result = GraphClient.ServicePrincipals.List(new Rest.Azure.OData.ODataQuery<ServicePrincipal>(s => s.DisplayName.StartsWith(options.SearchString)));
                    }
                    else
                    {
                        result = GraphClient.ServicePrincipals.ListNext(options.NextLink);
                    }

                    servicePrincipals.AddRange(result.Select(u => u.ToPSADServicePrincipal()));
                    options.NextLink = result.NextPageLink;
                }
                else
                {
                    result = GraphClient.ServicePrincipals.List(new Rest.Azure.OData.ODataQuery<ServicePrincipal>(s => s.DisplayName.StartsWith(options.SearchString)));
                    servicePrincipals.AddRange(result.Select(u => u.ToPSADServicePrincipal()));

                    while (!string.IsNullOrEmpty(result.NextPageLink))
                    {
                        result = GraphClient.ServicePrincipals.ListNext(result.NextPageLink);
                        servicePrincipals.AddRange(result.Select(u => u.ToPSADServicePrincipal()));
                    }
                }
            }

            return servicePrincipals;
        }