public override void ExecuteCmdlet() { ExecutionBlock(() => { ADObjectFilterOptions options = new ADObjectFilterOptions { SearchString = this.IsParameterBound(c => c.StartsWith) ? StartsWith + "*" : DisplayName, UPN = UserPrincipalName, Id = ObjectId, Paging = true, Mail = Mail }; if (!string.IsNullOrEmpty(options.SearchString)) { //query string is wrapped with single quote. Escape is needed if it contains additional quote. options.SearchString = options.SearchString.Replace("'", "''"); } ulong first = MyInvocation.BoundParameters.ContainsKey("First") ? this.PagingParameters.First : ulong.MaxValue; ulong skip = MyInvocation.BoundParameters.ContainsKey("Skip") ? this.PagingParameters.Skip : 0; WriteObject(ActiveDirectoryClient.FilterUsers(options, first, skip), true); }); }
public IEnumerable <PSADUser> FilterUsers(ADObjectFilterOptions options, ulong first = ulong.MaxValue, ulong skip = 0) { if (!string.IsNullOrEmpty(options.Id)) { User user = null; try { user = GraphClient.Users.Get(Normalize(options.Id)); } catch { /* The user does not exist, ignore the exception. */ } if (user != null) { return(new List <PSADUser> { user.ToPSADUser() }); } } else if (!string.IsNullOrEmpty(options.UPN) || !string.IsNullOrEmpty(options.Mail)) { IPage <User> result = null; try { string upnOrMail = Normalize(options.UPN) ?? Normalize(options.Mail); var odataQuery = new Rest.Azure.OData.ODataQuery <User>(); if (!string.IsNullOrEmpty(options.UPN)) { odataQuery.SetFilter(u => u.UserPrincipalName == upnOrMail); } else { odataQuery.SetFilter(u => u.Mail == upnOrMail); } result = GraphClient.Users.List(odataQuery); } catch { /* The user does not exist, ignore the exception. */ } if (result != null) { return(result.Select(u => u.ToPSADUser())); } } else { Rest.Azure.OData.ODataQuery <User> odataQuery = null; if (!string.IsNullOrEmpty(options.SearchString) && options.SearchString.EndsWith("*")) { options.SearchString = options.SearchString.TrimEnd('*'); odataQuery = new Rest.Azure.OData.ODataQuery <User>(u => u.DisplayName != null && u.DisplayName.StartsWith(options.SearchString)); } else { odataQuery = new Rest.Azure.OData.ODataQuery <User>(u => u.DisplayName == options.SearchString); } return(new GenericPageEnumerable <User>( delegate() { return GraphClient.Users.List(odataQuery.ToString()); }, GraphClient.Users.ListNext, first, skip).Select(u => u.ToPSADUser())); } return(new List <PSADUser>()); }
public IEnumerable <PSADUser> FilterUsers(ADObjectFilterOptions options, int first = int.MaxValue, int skip = 0) { if (!string.IsNullOrEmpty(options.Id)) { MicrosoftGraphUser user = null; try { user = GraphClient.Users.GetUser(Normalize(options.Id)); } catch { /* The user does not exist, ignore the exception. */ } if (user != null) { return(new List <PSADUser> { user.ToPSADUser() }); } } else if (!string.IsNullOrEmpty(options.UPN) || !string.IsNullOrEmpty(options.Mail)) { IList <MicrosoftGraphUser> result = null; try { string upnOrMail = Normalize(options.UPN) ?? Normalize(options.Mail); var odataQuery = new Rest.Azure.OData.ODataQuery <MicrosoftGraphUser>(); if (!string.IsNullOrEmpty(options.UPN)) { odataQuery.SetFilter(u => u.UserPrincipalName == upnOrMail); } else { odataQuery.SetFilter(u => u.Mail == upnOrMail); } result = GraphClient.Users.ListUser( "eventual", filter: OdataHelper.GetFilterString(odataQuery) ).Value; } catch { /* The user does not exist, ignore the exception. */ } if (result != null) { return(result.Select(u => u.ToPSADUser())); } } else { Rest.Azure.OData.ODataQuery <MicrosoftGraphUser> odataQuery = null; if (!string.IsNullOrEmpty(options.SearchString) && options.SearchString.EndsWith("*")) { options.SearchString = options.SearchString.TrimEnd('*'); odataQuery = new Rest.Azure.OData.ODataQuery <MicrosoftGraphUser>(u => u.DisplayName != null && u.DisplayName.StartsWith(options.SearchString)); } else { odataQuery = new Rest.Azure.OData.ODataQuery <MicrosoftGraphUser>(u => u.DisplayName == options.SearchString); } return(GraphClient.Users.ListUser( consistencyLevel: "eventual", filter: OdataHelper.GetFilterString(odataQuery) ).Value.Select(u => u.ToPSADUser())); } return(new List <PSADUser>()); }