/// <summary>
        /// Performs the operations associated with the command.
        /// </summary>
        protected override void ProcessRecord()
        {
            AzureAccount        account = new AzureAccount();
            IPartner            partnerOperations;
            OrganizationProfile profile;

            WriteDebug(string.Format(CultureInfo.CurrentCulture, Resources.ConnectPartnerCenterBeginProcess, ParameterSetName));

            PartnerService.Instance.EnforceMfa = (EnforceMFA.IsPresent && EnforceMFA.ToBool());

            if (ParameterSetName.Equals(AccessTokenParameterSet, StringComparison.InvariantCultureIgnoreCase))
            {
                account.Id = ApplicationId;
                account.Properties[AzureAccountPropertyType.AccessToken] = AccessToken;
                account.Type = AccountType.AccessToken;
            }
            else if (ParameterSetName.Equals(ServicePrincipalParameterSet, StringComparison.InvariantCultureIgnoreCase))
            {
                account.Id = string.IsNullOrEmpty(ApplicationId) ? Credential.UserName : ApplicationId;
                account.Properties[AzureAccountPropertyType.ServicePrincipalSecret] = Credential.Password.ConvertToString();
                account.Type = AccountType.ServicePrincipal;
            }
            else
            {
                account.Type = AccountType.User;
            }

            account.Properties[AzureAccountPropertyType.Tenant] = string.IsNullOrEmpty(TenantId) ? AuthenticationConstants.CommonEndpoint : TenantId;

            PartnerSession.Instance.Context = new PartnerContext
            {
                Account       = account,
                ApplicationId = ApplicationId,
                Environment   = Environment
            };

            PartnerSession.Instance.AuthenticationFactory.Authenticate(
                PartnerSession.Instance.Context,
                d => WriteDebug(d),
                w => WriteWarning(w));

            if (PartnerSession.Instance.Context.AuthenticationType == AuthenticationTypes.AppPlusUser)
            {
                partnerOperations = PartnerSession.Instance.ClientFactory.CreatePartnerOperations(d => WriteDebug(d));
                profile           = partnerOperations.Profiles.OrganizationProfile.GetAsync().GetAwaiter().GetResult();

                PartnerSession.Instance.Context.CountryCode = profile.DefaultAddress.Country;
                PartnerSession.Instance.Context.Locale      = profile.Culture;
            }

            WriteObject(PartnerSession.Instance.Context);
        }
        /// <summary>
        /// Performs the operations associated with the command.
        /// </summary>
        protected override void ProcessRecord()
        {
            IPartner            partnerOperations;
            OrganizationProfile profile;
            PartnerAccount      account     = new PartnerAccount();
            PartnerEnvironment  environment = PartnerEnvironment.PublicEnvironments[Environment];

            PartnerService.Instance.EnforceMfa = (EnforceMFA.IsPresent && EnforceMFA.ToBool());

            if (!string.IsNullOrEmpty(CertificateThumbprint))
            {
                account.SetProperty(PartnerAccountPropertyType.CertificateThumbprint, CertificateThumbprint);
            }

            if (!string.IsNullOrEmpty(RefreshToken))
            {
                account.SetProperty(PartnerAccountPropertyType.RefreshToken, RefreshToken);
            }

            account.SetProperty(PartnerAccountPropertyType.ApplicationId, PowerShellApplicationId);

            if (ParameterSetName.Equals(AccessTokenParameterSet, StringComparison.InvariantCultureIgnoreCase))
            {
                account.SetProperty(PartnerAccountPropertyType.AccessToken, AccessToken);
                account.Type = AccountType.AccessToken;
            }
            else if (ParameterSetName.Equals(RefreshTokenParameterSet, StringComparison.InvariantCultureIgnoreCase))
            {
                if (Credential != null)
                {
                    account.ObjectId = Credential.UserName;
                    account.SetProperty(PartnerAccountPropertyType.ApplicationId, Credential.UserName);
                    account.SetProperty(PartnerAccountPropertyType.ServicePrincipalSecret, Credential.Password.ConvertToString());
                }
            }
            else if (ParameterSetName.Equals(ServicePrincipalCertificateParameterSet, StringComparison.InvariantCultureIgnoreCase))
            {
                account.SetProperty(PartnerAccountPropertyType.ApplicationId, ApplicationId);
            }
            else if (ParameterSetName.Equals(ServicePrincipalParameterSet, StringComparison.InvariantCultureIgnoreCase))
            {
                account.ObjectId = Credential.UserName;
                account.Type     = AccountType.ServicePrincipal;

                account.SetProperty(PartnerAccountPropertyType.ApplicationId, Credential.UserName);
                account.SetProperty(PartnerAccountPropertyType.ServicePrincipalSecret, Credential.Password.ConvertToString());
            }
            else
            {
                account.Type = AccountType.User;
            }

            if (UseDeviceAuthentication.IsPresent)
            {
                account.SetProperty("UseDeviceAuth", "true");
            }

            account.SetProperty(
                PartnerAccountPropertyType.Scope,
                ParameterSetName.Equals(ServicePrincipalParameterSet, StringComparison.InvariantCultureIgnoreCase) ?
                $"{environment.AzureAdGraphEndpoint}/.default" :
                $"{environment.PartnerCenterEndpoint}/user_impersonation");

            account.Tenant = string.IsNullOrEmpty(Tenant) ? "common" : Tenant;

            PartnerSession.Instance.AuthenticationFactory.Authenticate(
                account,
                environment,
                new[] { account.GetProperty(PartnerAccountPropertyType.Scope) },
                Message);

            PartnerSession.Instance.Context = new PartnerContext
            {
                Account     = account,
                Environment = environment
            };

            try
            {
                partnerOperations = PartnerSession.Instance.ClientFactory.CreatePartnerOperations();
                profile           = partnerOperations.Profiles.OrganizationProfile.GetAsync().GetAwaiter().GetResult();

                PartnerSession.Instance.Context.CountryCode = profile.DefaultAddress.Country;
                PartnerSession.Instance.Context.Locale      = profile.Culture;
            }
            catch (PartnerException)
            {
                /* This error can safely be ignored */
            }

            WriteObject(PartnerSession.Instance.Context);
        }