/// <summary>
        /// Creates a new migration batch in Exchange Online.
        /// </summary>
        /// <param name="entity">An instance of <see cref="MigrationBatchEntity"/> that represents the new migration batch.</param>
        /// <returns>An instance of <see cref="Task"/> that represents the asynchronous operation.</returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="entity"/> is null.
        /// </exception>
        public async Task CreateMigrationBatchAsync(MigrationBatchEntity entity)
        {
            Command command;
            CommandParameterCollection parameters;
            EnvironmentEntity          environment;
            List <MailboxEntity>       entites;;
            PSCredential        credential;
            Uri                 connectionUri;
            WSManConnectionInfo connectionInfo;
            string              csvData;

            entity.AssertNotNull(nameof(entity));

            try
            {
                credential = new PSCredential(
                    service.Configuration.ExchangeOnlineUsername,
                    service.Configuration.ExchangeOnlinePassword.ToSecureString());

                environment = await service.Storage.GetEntityAsync <EnvironmentEntity>(
                    MigrationConstants.EnvironmentTableName,
                    entity.CustomerId,
                    entity.PartitionKey);

                connectionUri  = new Uri($"{MigrationConstants.ExchangeOnlineEndpoint}{environment.Organization}");
                connectionInfo = GetConnectionInfo(connectionUri, MigrationConstants.SchemaUri, credential);

                entites = await service.Storage.GetEntitiesAsync <MailboxEntity>(
                    MigrationConstants.MailboxTableName,
                    m => m.PartitionKey.Equals(environment.RowKey) &&
                    !string.IsNullOrEmpty(m.MigrationBatchId) &&
                    m.MigrationBatchId.Equals(entity.RowKey));

                csvData = entites.Aggregate(new StringBuilder("EmailAddress\n"),
                                            (sb, v) => sb.Append(v.PrimarySmtpAddress).Append("\n"),
                                            sb => { if (0 < sb.Length)
                                                    {
                                                        sb.Length--;
                                                    }
                                                    return(sb.ToString()); });

                command = new Command("New-MigrationBatch");

                parameters = new CommandParameterCollection
                {
                    { "CSVData", Encoding.ASCII.GetBytes(csvData) },
                    { "Name", entity.Name },
                    { "SourceEndpoint", environment.Name },
                    { "TargetDeliveryDomain", entity.TargetDeliveryDomain }
                };

                using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
                {
                    scriptManager.InvokeCommand(runspace, command, parameters);
                }
            }
            finally
            {
                connectionInfo = null;
                connectionUri  = null;
                credential     = null;
                parameters     = null;
            }
        }