/// <summary>
        /// Gets a list of deleted users from Partner Center.
        /// </summary>
        /// <param name="customerId">Identifier of the customer.</param>
        /// <exception cref="ArgumentException">
        /// <paramref name="customerId"/> is empty or null.
        /// </exception>
        private List <CustomerUser> GetDeletedUsers(string customerId)
        {
            SimpleFieldFilter filter = new SimpleFieldFilter("UserState", FieldFilterOperation.Equals, "Inactive");
            IQuery            simpleQueryWithFilter = QueryFactory.Instance.BuildSimpleQuery(filter);
            IResourceCollectionEnumerator <SeekBasedResourceCollection <CustomerUser> > usersEnumerator;
            List <CustomerUser> users;
            SeekBasedResourceCollection <CustomerUser> seekUsers;

            customerId.AssertNotEmpty(nameof(customerId));

            try
            {
                users = new List <CustomerUser>();

                seekUsers       = Partner.Customers[customerId].Users.Query(simpleQueryWithFilter);
                usersEnumerator = Partner.Enumerators.CustomerUsers.Create(seekUsers);
                while (usersEnumerator.HasValue)
                {
                    users.AddRange(usersEnumerator.Current.Items);
                    usersEnumerator.Next();
                }

                return(users);
            }
            finally
            {
                users           = null;
                seekUsers       = null;
                usersEnumerator = null;
            }
        }
예제 #2
0
        /// <summary>
        /// Executes the operations associated with the cmdlet.
        /// </summary>
        public override void ExecuteCmdlet()
        {
            Scheduler.RunTask(async() =>
            {
                string value = string.IsNullOrEmpty(UserId) ? UserPrincipalName : UserId;

                if (ShouldProcess(string.Format(CultureInfo.CurrentCulture, Resources.RestorePartnerCustomerUserWhatIf, value)))
                {
                    IPartner partner = await PartnerSession.Instance.ClientFactory.CreatePartnerOperationsAsync(CorrelationId, CancellationToken).ConfigureAwait(false);
                    string userId;

                    if (ParameterSetName.Equals("ByUpn", StringComparison.InvariantCultureIgnoreCase))
                    {
                        SimpleFieldFilter filter     = new SimpleFieldFilter("UserState", FieldFilterOperation.Equals, "Inactive");
                        IQuery simpleQueryWithFilter = QueryFactory.BuildSimpleQuery(filter);
                        IResourceCollectionEnumerator <SeekBasedResourceCollection <CustomerUser> > usersEnumerator;
                        List <CustomerUser> users = new List <CustomerUser>();
                        SeekBasedResourceCollection <CustomerUser> seekUsers;

                        seekUsers       = await partner.Customers[CustomerId].Users.QueryAsync(simpleQueryWithFilter, CancellationToken).ConfigureAwait(false);
                        usersEnumerator = partner.Enumerators.CustomerUsers.Create(seekUsers);

                        while (usersEnumerator.HasValue)
                        {
                            users.AddRange(usersEnumerator.Current.Items);
                            await usersEnumerator.NextAsync(RequestContextFactory.Create(CorrelationId), CancellationToken).ConfigureAwait(false);
                        }

                        CustomerUser user = users.SingleOrDefault(u => string.Equals(u.UserPrincipalName, UserPrincipalName, StringComparison.CurrentCultureIgnoreCase));

                        if (user == null)
                        {
                            throw new PSInvalidOperationException($"Unable to locate {UserPrincipalName}");
                        }

                        userId = user.Id;
                    }
                    else
                    {
                        userId = UserId;
                    }

                    CustomerUser updatedCustomerUser = new CustomerUser
                    {
                        State = UserState.Active
                    };

                    await partner.Customers.ById(CustomerId).Users.ById(userId).PatchAsync(updatedCustomerUser, CancellationToken).ConfigureAwait(false);
                }
            }, true);
        }
        public async Task <string> GetOffice365UserIdAsync(string userPrincipalName, string office365CustomerId)
        {
            var filter = new SimpleFieldFilter("UserPrincipalName",
                                               FieldFilterOperation.StartsWith, userPrincipalName);
            var qurey = QueryFactory.Instance.BuildSimpleQuery(filter);

            var queryResult = await _partnerOperations.UserPartnerOperations.Customers.ById(office365CustomerId).Users
                              .QueryAsync(qurey);

            if (queryResult.Items.Count() == 1) // We need to ensure that only one result is returned. Anything else and it is false positive
            {
                return(queryResult.Items.FirstOrDefault()?.Id);
            }

            return(null);
        }
예제 #4
0
        /// <summary>
        /// Executes the scenario.
        /// </summary>
        protected override void RunScenario()
        {
            var indirectResellerId = this.ObtainIndirectResellerId("Enter the ID of the indirect reseller: ");

            var partnerOperations = this.Context.UserPartnerOperations;

            this.Context.ConsoleHelper.StartProgress("Getting customers of the indirect reseller");

            // Create a simple field filter.
            var filter = new SimpleFieldFilter(
                CustomerSearchField.IndirectReseller.ToString(),
                FieldFilterOperation.StartsWith,
                indirectResellerId);

            // Create an iQuery object to pass to the Query method.
            var myQuery = QueryFactory.Instance.BuildSimpleQuery(filter);

            // Get the collection of matching customers.
            var customersPage = partnerOperations.Customers.Query(myQuery);

            this.Context.ConsoleHelper.StopProgress();

            // Create a customer enumerator which will aid us in traversing the customer pages.
            var customersEnumerator = partnerOperations.Enumerators.Customers.Create(customersPage);
            int pageNumber          = 1;

            Console.WriteLine(string.Format(CultureInfo.InvariantCulture, "Customers of indirect reseller: {0}", indirectResellerId));

            while (customersEnumerator.HasValue)
            {
                // Print the current customer results page.
                this.Context.ConsoleHelper.WriteObject(customersEnumerator.Current, string.Format(CultureInfo.InvariantCulture, "Customer Page: {0}", pageNumber++));

                Console.WriteLine();
                Console.Write("Press any key to retrieve the next customers page");
                Console.ReadKey();

                this.Context.ConsoleHelper.StartProgress("Getting next customers page");

                // Get the next page of customers.
                customersEnumerator.Next();

                this.Context.ConsoleHelper.StopProgress();
                Console.Clear();
            }
        }
        /// <summary>
        /// Executes the scenario.
        /// </summary>
        protected override void RunScenario()
        {
            var    partnerOperations = this.Context.UserPartnerOperations;
            string searchPrefix      = this.Context.ConsoleHelper.ReadNonEmptyString("Enter the prefix to search for", "The entered prefix is empty");

            this.Context.ConsoleHelper.StartProgress("Filtering");

            var fieldFilter = new SimpleFieldFilter(
                this.customerSearchField.ToString(),
                FieldFilterOperation.StartsWith,
                searchPrefix);

            var myQuery = QueryFactory.Instance.BuildSimpleQuery(fieldFilter);

            var customers = partnerOperations.Customers.Query(myQuery);

            this.Context.ConsoleHelper.StopProgress();
            this.Context.ConsoleHelper.WriteObject(customers, "Customer matches");
        }
예제 #6
0
        /// <summary>
        /// Executes the scenario.
        /// </summary>
        protected override void RunScenario()
        {
            // get customer Id of the entered customer user.
            string selectedCustomerId = this.ObtainCustomerId("Enter the ID of the customer to get all inactive customer users in pages");

            var partnerOperations = this.Context.UserPartnerOperations;

            // get customer user page size
            string customerUserPageSize = this.ObtainCustomerUserPageSize();

            this.customerUserPageSize = int.Parse(customerUserPageSize);

            var filter = new SimpleFieldFilter("UserStatus", FieldFilterOperation.Equals, "Inactive");

            // Read inactive customer users in a batch
            this.Context.ConsoleHelper.StartProgress("Querying first page of inactive customer users");
            var simpleQueryWithFilter = QueryFactory.Instance.BuildIndexedQuery(this.customerUserPageSize, 0, filter);
            var customerUsers         = partnerOperations.Customers.ById(selectedCustomerId).Users.Query(simpleQueryWithFilter);

            this.Context.ConsoleHelper.StopProgress();

            this.Context.ConsoleHelper.StartProgress("Creating customer user Enumerator");
            var customerUsersEnumerator = partnerOperations.Enumerators.CustomerUsers.Create(customerUsers);

            this.Context.ConsoleHelper.StopProgress();

            while (customerUsersEnumerator.HasValue)
            {
                // print the current customer user results
                this.Context.ConsoleHelper.WriteObject(customerUsersEnumerator.Current);

                Console.WriteLine();
                Console.Write("Press any key to retrieve the next customer users page");
                Console.ReadKey();
                Console.WriteLine();
                Console.WriteLine("Getting Next Page");

                // get the next page of customer users
                customerUsersEnumerator.Next();
                Console.Clear();
            }
        }
        /// <summary>
        /// Executes the scenario.
        /// </summary>
        protected override void RunScenario()
        {
            var partnerOperations = this.Context.UserPartnerOperations;

            string customerId = this.Context.ConsoleHelper.ReadNonEmptyString("Enter a Customer Id to search for", "No Customer Id entered");
            var    startDate  = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 01);

            this.Context.ConsoleHelper.StartProgress(
                string.Format(CultureInfo.InvariantCulture, "Retrieving the partner's audit records - start date: {0}", startDate));

            var filter = new SimpleFieldFilter(AuditRecordSearchField.CustomerId.ToString(), FieldFilterOperation.Equals, customerId);

            var auditRecordsPage = partnerOperations.AuditRecords.Query(startDate.Date, query: QueryFactory.Instance.BuildSimpleQuery(filter));

            this.Context.ConsoleHelper.StopProgress();

            // create a customer enumerator which will aid us in traversing the customer pages
            var auditRecordEnumerator = partnerOperations.Enumerators.AuditRecords.Create(auditRecordsPage);

            int pageNumber = 1;

            while (auditRecordEnumerator.HasValue)
            {
                // print the current audit record results page
                this.Context.ConsoleHelper.WriteObject(auditRecordEnumerator.Current, string.Format(CultureInfo.InvariantCulture, "Audit Record Page: {0}", pageNumber++));

                Console.WriteLine();
                Console.Write("Press any key to retrieve the next set of audit records");
                Console.ReadKey();

                this.Context.ConsoleHelper.StartProgress("Getting next audit records page");

                // get the next page of audit records
                auditRecordEnumerator.Next();

                this.Context.ConsoleHelper.StopProgress();
                Console.Clear();
            }
        }
예제 #8
0
        /// <summary>
        /// Executes the operations associated with the cmdlet.
        /// </summary>
        public override void ExecuteCmdlet()
        {
            Scheduler.RunTask(async() =>
            {
                IPartner partner = await PartnerSession.Instance.ClientFactory.CreatePartnerOperationsAsync(CorrelationId, CancellationToken).ConfigureAwait(false);

                if (ParameterSetName.Equals("ByUserId", StringComparison.InvariantCultureIgnoreCase))
                {
                    CustomerUser customerUser = await partner.Customers[CustomerId].Users[UserId].GetAsync(CancellationToken).ConfigureAwait(false);
                    WriteObject(new PSCustomerUser(customerUser));
                }
                else if (ParameterSetName.Equals("ByUserState", StringComparison.InvariantCultureIgnoreCase) && ReturnDeletedUsers.ToBool())
                {
                    SimpleFieldFilter filter     = new SimpleFieldFilter("UserState", FieldFilterOperation.Equals, "Inactive");
                    IQuery simpleQueryWithFilter = QueryFactory.BuildSimpleQuery(filter);
                    IResourceCollectionEnumerator <SeekBasedResourceCollection <CustomerUser> > usersEnumerator;
                    List <CustomerUser> users;
                    SeekBasedResourceCollection <CustomerUser> seekUsers;

                    users = new List <CustomerUser>();

                    seekUsers       = await partner.Customers[CustomerId].Users.QueryAsync(simpleQueryWithFilter, CancellationToken).ConfigureAwait(false);
                    usersEnumerator = partner.Enumerators.CustomerUsers.Create(seekUsers);

                    while (usersEnumerator.HasValue)
                    {
                        users.AddRange(usersEnumerator.Current.Items);
                        await usersEnumerator.NextAsync(RequestContextFactory.Create(CorrelationId), CancellationToken).ConfigureAwait(false);
                    }

                    WriteObject(users.Select(u => new PSCustomerUser(u)), true);
                }
                else if (ParameterSetName.Equals("ByUpn", StringComparison.InvariantCultureIgnoreCase))
                {
                    GraphServiceClient client     = PartnerSession.Instance.ClientFactory.CreateGraphServiceClient() as GraphServiceClient;
                    client.AuthenticationProvider = new GraphAuthenticationProvider(CustomerId);

                    Graph.User user           = await client.Users[UserPrincipalName].Request().GetAsync(CancellationToken).ConfigureAwait(false);
                    CustomerUser customerUser = await partner.Customers[CustomerId].Users[user.Id].GetAsync(CancellationToken).ConfigureAwait(false);

                    WriteObject(customerUser);
                }
                else
                {
                    IResourceCollectionEnumerator <SeekBasedResourceCollection <CustomerUser> > usersEnumerator;
                    List <CustomerUser> users;
                    SeekBasedResourceCollection <CustomerUser> seekUsers;

                    users = new List <CustomerUser>();

                    seekUsers       = await partner.Customers[CustomerId].Users.GetAsync(CancellationToken).ConfigureAwait(false);
                    usersEnumerator = partner.Enumerators.CustomerUsers.Create(seekUsers);

                    while (usersEnumerator.HasValue)
                    {
                        users.AddRange(usersEnumerator.Current.Items);
                        await usersEnumerator.NextAsync(RequestContextFactory.Create(CorrelationId), CancellationToken).ConfigureAwait(false);
                    }

                    WriteObject(users.Select(u => new PSCustomerUser(u)), true);
                }
            }, true);
        }
예제 #9
0
        /// <summary>
        /// Executes the scenario.
        /// </summary>
        protected override void RunScenario()
        {
            var partnerOperations = this.Context.UserPartnerOperations;

            string[] resourceTypes = Enum.GetNames(typeof(ResourceType));

            int resourceTypesMaxIndex;

            for (resourceTypesMaxIndex = 0; resourceTypesMaxIndex < resourceTypes.Length; resourceTypesMaxIndex++)
            {
                Console.WriteLine(resourceTypesMaxIndex.ToString() + ": " + resourceTypes[resourceTypesMaxIndex]);
            }

            // decrement max index by 1 to account for zero-based start
            resourceTypesMaxIndex--;

            Console.WriteLine();

            while (true)
            {
                string resourceType = this.Context.ConsoleHelper.ReadNonEmptyString(
                    "Enter the number corresponding to the Resource Type to search for",
                    string.Format(CultureInfo.InvariantCulture, "Please enter a number between 0 and {0}", resourceTypesMaxIndex));

                int resourceTypeInt = -1;

                if (int.TryParse(resourceType, out resourceTypeInt))
                {
                    if (resourceTypeInt < 0 || resourceTypeInt > resourceTypesMaxIndex)
                    {
                        this.Context.ConsoleHelper.Error(
                            string.Format(CultureInfo.InvariantCulture, "The number must be between 0 and {0}", resourceTypesMaxIndex));

                        break;
                    }
                }
                else
                {
                    this.Context.ConsoleHelper.Error(
                        string.Format(CultureInfo.InvariantCulture, "The number must be a valid integer between 0 and {0}", resourceTypesMaxIndex));

                    break;
                }

                var startDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 01);

                this.Context.ConsoleHelper.StartProgress(
                    string.Format(CultureInfo.InvariantCulture, "Retrieving the partner's audit records - start date: {0}", startDate));

                string searchField = Enum.GetName(typeof(ResourceType), resourceTypeInt);
                var    filter      = new SimpleFieldFilter(AuditRecordSearchField.ResourceType.ToString(), FieldFilterOperation.Equals, searchField);

                var auditRecordsPage = partnerOperations.AuditRecords.Query(startDate.Date, query: QueryFactory.Instance.BuildSimpleQuery(filter));

                this.Context.ConsoleHelper.StopProgress();

                // create a customer enumerator which will aid us in traversing the customer pages
                var auditRecordEnumerator = partnerOperations.Enumerators.AuditRecords.Create(auditRecordsPage);

                int pageNumber = 1;

                while (auditRecordEnumerator.HasValue)
                {
                    // print the current audit record results page
                    this.Context.ConsoleHelper.WriteObject(auditRecordEnumerator.Current, string.Format(CultureInfo.InvariantCulture, "Audit Record Page: {0}", pageNumber++));

                    Console.WriteLine();
                    Console.Write("Press any key to retrieve the next set of audit records");
                    Console.ReadKey();

                    this.Context.ConsoleHelper.StartProgress("Getting next audit records page");

                    // get the next page of audit records
                    auditRecordEnumerator.Next();

                    this.Context.ConsoleHelper.StopProgress();
                    Console.Clear();
                }

                break;
            }
        }