Пример #1
0
        public void UpdateMailbox(UsersObject user, MailboxPlanObject mailboxPlan)
        {
            try
            {
                this.logger.Debug("Updating mailbox for " + user.UserPrincipalName);

                PSCommand cmd = new PSCommand();
                cmd.AddCommand("Set-Mailbox");
                cmd.AddParameter("Identity", user.UserPrincipalName);
                cmd.AddParameter("CustomAttribute1", user.CompanyCode);
                cmd.AddParameter("DeliverToMailboxAndForward", user.DeliverToMailboxAndForward);
                cmd.AddParameter("HiddenFromAddressListsEnabled", user.MailboxHiddenFromGAL);
                cmd.AddParameter("IssueWarningQuota", mailboxPlan.WarningSizeInMB(user.SetMailboxSizeInMB));
                cmd.AddParameter("ProhibitSendQuota", user.SetMailboxSizeInMB + "MB");
                cmd.AddParameter("ProhibitSendReceiveQuota", user.SetMailboxSizeInMB + "MB");
                cmd.AddParameter("MaxReceiveSize", mailboxPlan.MaxReceiveInKB + "KB");
                cmd.AddParameter("MaxSendSize", mailboxPlan.MaxSendInKB + "KB");
                cmd.AddParameter("RecipientLimits", mailboxPlan.MaxRecipients);
                cmd.AddParameter("RetainDeletedItemsFor", mailboxPlan.MaxKeepDeletedItemsInDays);
                cmd.AddParameter("RetainDeletedItemsUntilBackup", true);
                cmd.AddParameter("UseDatabaseQuotaDefaults", false);
                cmd.AddParameter("OfflineAddressBook", user.CompanyCode + " OAL");

                List<string> emailAddresses = new List<string>();
                emailAddresses.Add("SMTP:" + user.PrimarySmtpAddress);
                foreach (string e in user.EmailAliases)
                {
                    if (e.StartsWith("sip:", StringComparison.CurrentCultureIgnoreCase))
                        emailAddresses.Add(e);
                    else if (e.StartsWith("x500:", StringComparison.CurrentCultureIgnoreCase))
                        emailAddresses.Add(e);
                    else if (e.StartsWith("x400:", StringComparison.CurrentCultureIgnoreCase))
                        emailAddresses.Add(e);
                    else
                        emailAddresses.Add("smtp:" + e);
                }
                cmd.AddParameter("EmailAddresses", emailAddresses.ToArray());

                if (!string.IsNullOrEmpty(user.ForwardingTo))
                    cmd.AddParameter("ForwardingAddress", user.ForwardingTo);

                if (!string.IsNullOrEmpty(user.ThrottlingPolicy))
                    cmd.AddParameter("ThrottlingPolicy", user.ThrottlingPolicy);

                cmd.AddParameter("Confirm", false);
                cmd.AddParameter("DomainController", domainController);
                powershell.Commands = cmd;

                Collection<PSObject> obj = powershell.Invoke();
                if (powershell.HadErrors)
                    throw powershell.Streams.Error[0].Exception;
            }
            catch (Exception ex)
            {
                this.logger.Error("Failed to update mailbox for " + user.UserPrincipalName, ex);
                throw;
            }
        }