Ejemplo n.º 1
0
        private static void UpdateRecipients(EmailAddressPolicy eap, OrganizationId organizationId, string domainController, IRecipientSession globalCatalogSession, Task.TaskVerboseLoggingDelegate writeVerbose, Task.TaskWarningLoggingDelegate writeWarning, WriteProgress writeProgress, Task cmdlet, bool fixMissingAlias)
        {
            UpdateEmailAddressPolicy.AssertArgumentNotNull(eap, "eap");
            UpdateEmailAddressPolicy.AssertArgumentNotNull(writeVerbose, "writeVerbose");
            UpdateEmailAddressPolicy.AssertArgumentNotNull(writeWarning, "writeWarning");
            UpdateEmailAddressPolicy.AssertArgumentNotNull(writeProgress, "writeProgress");
            if (string.IsNullOrEmpty(eap.LdapRecipientFilter) && !fixMissingAlias)
            {
                return;
            }
            int num = 0;

            try
            {
                if (cmdlet != null && cmdlet.Stopping)
                {
                    return;
                }
                IRecipientSession tenantOrRootOrgRecipientSession = DirectorySessionFactory.Default.GetTenantOrRootOrgRecipientSession(false, ConsistencyMode.PartiallyConsistent, globalCatalogSession.SessionSettings, 409, "UpdateRecipients", "f:\\15.00.1497\\sources\\dev\\Management\\src\\Management\\SystemConfigurationTasks\\RecipientPolicy\\UpdateEmailAddressPolicy.cs");
                tenantOrRootOrgRecipientSession.EnforceDefaultScope = false;
                IEnumerable <ADRecipient> enumerable = eap.FindMatchingRecipientsPaged(globalCatalogSession, organizationId, null, fixMissingAlias);
                string    text      = null;
                Hashtable hashtable = new Hashtable();
                foreach (ADRecipient adrecipient in enumerable)
                {
                    if (cmdlet != null && cmdlet.Stopping)
                    {
                        return;
                    }
                    if (!string.IsNullOrEmpty(domainController) && string.IsNullOrEmpty(text))
                    {
                        try
                        {
                            string configurationDomainControllerFqdn = SystemConfigurationTasksHelper.GetConfigurationDomainControllerFqdn(domainController);
                            int    num2 = configurationDomainControllerFqdn.IndexOf(".");
                            if (0 <= num2)
                            {
                                text = configurationDomainControllerFqdn.Substring(num2);
                            }
                        }
                        catch (SocketException ex)
                        {
                            writeWarning(Strings.ErrorResolveFqdnForDomainController(domainController, ex.Message));
                            return;
                        }
                    }
                    string text2 = adrecipient.Id.DomainId.DistinguishedName.ToLowerInvariant();
                    if (!hashtable.ContainsKey(text2))
                    {
                        SystemConfigurationTasksHelper.PrepareDomainControllerRecipientSessionForUpdate(tenantOrRootOrgRecipientSession, adrecipient.Id, domainController, text);
                        IEnumerable <ADRecipient> collection = eap.FindMatchingRecipientsPaged(tenantOrRootOrgRecipientSession, organizationId, adrecipient.Id, fixMissingAlias);
                        List <ADRecipient>        list       = new List <ADRecipient>();
                        Exception ex2 = null;
                        Exception ex3 = null;
                        try
                        {
                            list.AddRange(collection);
                        }
                        catch (DataSourceOperationException ex4)
                        {
                            TaskLogger.Trace("Exception caught when re-read recipient from DC : {0}", new object[]
                            {
                                ex4.ToString()
                            });
                            if (ex4.InnerException is ActiveDirectoryObjectNotFoundException || ex4.InnerException is AuthenticationException)
                            {
                                ex3 = ex4;
                            }
                            else
                            {
                                ex2 = ex4;
                            }
                        }
                        catch (DataSourceTransientException ex5)
                        {
                            TaskLogger.Trace("Exception caught when re-read recipient from DC : {0}", new object[]
                            {
                                ex5.ToString()
                            });
                            if (ex5.InnerException is ActiveDirectoryOperationException || ex5.InnerException is ActiveDirectoryServerDownException)
                            {
                                ex3 = ex5;
                            }
                            else
                            {
                                ex2 = ex5;
                            }
                        }
                        if (ex3 != null)
                        {
                            hashtable.Add(text2, null);
                            writeWarning(Strings.ErrorCannotUpdateRecipientOfDomain(DNConvertor.FqdnFromDomainDistinguishedName(text2), ex3.Message));
                        }
                        else if (ex2 != null)
                        {
                            writeWarning(Strings.ErrorFailedToReadRecipientForUpdate(adrecipient.Id.ToString(), ex2.Message));
                        }
                        else if (1 == list.Count)
                        {
                            ADRecipient adrecipient2 = list[0];
                            if (cmdlet != null && cmdlet.Stopping)
                            {
                                return;
                            }
                            num = num++ % 99 + 1;
                            writeProgress(Strings.ProgressActivityUpdateRecipient, Strings.ProgressStatusUpdateRecipient(adrecipient2.Id.ToString()), num);
                            writeVerbose(Strings.ProgressStatusUpdateRecipient(adrecipient2.Id.ToString()));
                            try
                            {
                                if (fixMissingAlias && string.IsNullOrEmpty(adrecipient2.Alias))
                                {
                                    if (adrecipient2 is ADMicrosoftExchangeRecipient)
                                    {
                                        adrecipient2.Alias = RecipientTaskHelper.GenerateUniqueAlias(globalCatalogSession, adrecipient2.OrganizationId, ADMicrosoftExchangeRecipient.DefaultName, writeVerbose);
                                    }
                                    else if (adrecipient2 is ADSystemAttendantMailbox)
                                    {
                                        adrecipient2.Alias = RecipientTaskHelper.GenerateUniqueAlias(globalCatalogSession, adrecipient2.OrganizationId, (adrecipient2 as ADSystemAttendantMailbox).ServerName + "-SA", writeVerbose);
                                    }
                                    else
                                    {
                                        adrecipient2.Alias = RecipientTaskHelper.GenerateUniqueAlias(globalCatalogSession, adrecipient2.OrganizationId, adrecipient2.Name, writeVerbose);
                                    }
                                    writeWarning(Strings.WarningGeneratingMissingAlias(adrecipient2.Identity.ToString(), adrecipient2.Alias));
                                }
                                if (!adrecipient2.IsReadOnly)
                                {
                                    ProvisioningLayer.UpdateAffectedIConfigurable(cmdlet, RecipientTaskHelper.ConvertRecipientToPresentationObject(adrecipient2), true);
                                }
                                if (!adrecipient2.IsValid || adrecipient2.IsReadOnly)
                                {
                                    writeWarning(Strings.ErrorCannotUpdateInvalidRecipient(adrecipient2.Id.ToString()));
                                }
                                else
                                {
                                    if (cmdlet.IsVerboseOn && adrecipient2.ObjectState != ObjectState.Unchanged)
                                    {
                                        writeVerbose(TaskVerboseStringHelper.GetConfigurableObjectChangedProperties(adrecipient2));
                                    }
                                    tenantOrRootOrgRecipientSession.Save(adrecipient2);
                                }
                            }
                            catch (DataSourceTransientException ex6)
                            {
                                writeWarning(Strings.ErrorUpdateRecipient(adrecipient2.Id.ToString(), ex6.Message));
                                TaskLogger.Trace("Exception is raised while updating recipient '{0}': {1}", new object[]
                                {
                                    adrecipient2.Id.ToString(),
                                    ex6.Message
                                });
                            }
                            catch (DataSourceOperationException ex7)
                            {
                                writeWarning(Strings.ErrorUpdateRecipient(adrecipient2.Id.ToString(), ex7.Message));
                                TaskLogger.Trace("Exception is raised while updating recipient '{0}': {1}", new object[]
                                {
                                    adrecipient2.Id.ToString(),
                                    ex7.Message
                                });
                            }
                            catch (DataValidationException ex8)
                            {
                                writeWarning(Strings.ErrorUpdateRecipient(adrecipient2.Id.ToString(), ex8.Message));
                                TaskLogger.Trace("Exception is raised while updating recipient '{0}': {1}", new object[]
                                {
                                    adrecipient2.Id.ToString(),
                                    ex8.Message
                                });
                            }
                        }
                    }
                }
            }
            finally
            {
                if (cmdlet != null && cmdlet.Stopping)
                {
                    ExManagementApplicationLogger.LogEvent(ManagementEventLogConstants.Tuple_RecipientsUpdateForEmailAddressPolicyCancelled, new string[]
                    {
                        eap.Identity.ToString(),
                        eap.LdapRecipientFilter,
                        ADRecipientSchema.EmailAddresses.Name
                    });
                }
            }
            if (num != 0)
            {
                writeVerbose(Strings.ProgressStatusFinished);
                writeProgress(Strings.ProgressActivityUpdateRecipient, Strings.ProgressStatusFinished, 100);
            }
        }