示例#1
0
        private void Delete_DistributionGroup(string name)
        {
            ExchangePowershell powershell = null;

            try
            {
                this.logger.Warn("Rolling back action... Deleting distribution group " + name);

                powershell = new ExchangePowershell(StaticSettings.ExchangeURI, StaticSettings.Username, StaticSettings.DecryptedPassword, StaticSettings.ExchangeUseKerberos, StaticSettings.PrimaryDC);
                powershell.DeleteDistributionGroup(name);

                this.logger.Warn("Successfully removed distribution group " + name);
            }
            catch (Exception ex)
            {
                this.logger.Error("Failed to roll back action... Deleting distribution group " + name, ex);
            }
            finally
            {
                if (powershell != null)
                {
                    powershell.Dispose();
                }
            }
        }
示例#2
0
        private void Delete_Mailbox(string userPrincipalName)
        {
            ExchangePowershell powershell = null;

            try
            {
                this.logger.Warn("Rolling back action... Deleting Exchange mailbox " + userPrincipalName);

                powershell = new ExchangePowershell(StaticSettings.ExchangeURI, StaticSettings.Username, StaticSettings.DecryptedPassword, StaticSettings.ExchangeUseKerberos, StaticSettings.PrimaryDC);
                powershell.DeleteMailbox(userPrincipalName);

                this.logger.Warn("Successfully removed Exchange mailbox " + userPrincipalName);
            }
            catch (Exception ex)
            {
                this.logger.Error("Failed to roll back action... Deleting Exchange mailbox " + userPrincipalName, ex);
            }
            finally
            {
                if (powershell != null)
                {
                    powershell.Dispose();
                }
            }
        }
        public void DisableExchange(string companyCode)
        {
            ExchangePowershell powershell = null;
            CPDatabase         database   = null;

            try
            {
                powershell = new ExchangePowershell(StaticSettings.ExchangeURI, StaticSettings.Username, StaticSettings.DecryptedPassword, StaticSettings.ExchangeUseKerberos, StaticSettings.PrimaryDC);

                // Disable all exchange objects
                powershell.DeleteAllMailboxes(companyCode);
                powershell.DeleteAllContacts(companyCode);
                powershell.DeleteAllGroups(companyCode);
                powershell.DeleteAddressBookPolicy(companyCode + " ABP");
                powershell.DeleteOfflineAddressBook(companyCode + " OAL");
                powershell.DeleteAddressList(companyCode + " - All Rooms");
                powershell.DeleteAddressList(companyCode + " - All Contacts");
                powershell.DeleteAddressList(companyCode + " - All Groups");
                powershell.DeleteAddressList(companyCode + " - All Users");
                powershell.DeleteGlobalAddressList(companyCode + " - GAL");

                // Get all accepted domains
                this.logger.Debug("Retrieving list of accepted domains for " + companyCode);

                database = new CPDatabase();
                var domains = from d in database.Domains
                              where d.IsAcceptedDomain
                              where d.CompanyCode == companyCode
                              select d;

                if (domains != null)
                {
                    foreach (Domain d in domains)
                    {
                        powershell.DeleteDomain(d.Domain1);
                    }
                }

                // Now update the database
                int r = database.DisableExchange(companyCode);
                this.logger.Debug("Total count returned when calling DisableExchange stored procedure: " + r.ToString());
            }
            catch (Exception ex)
            {
                this.logger.Error("Error disabling Exchange for company " + companyCode, ex);
                ThrowEvent(Base.Enumerations.AlertID.FAILED, ex.Message);
            }
            finally
            {
                if (database != null)
                {
                    database.Dispose();
                }

                if (powershell != null)
                {
                    powershell.Dispose();
                }
            }
        }
示例#4
0
        public UsersObject GetUserMailbox(string identity)
        {
            ExchangePowershell powershell = null;

            try
            {
                powershell = new ExchangePowershell(StaticSettings.ExchangeURI, StaticSettings.Username, StaticSettings.DecryptedPassword, StaticSettings.ExchangeUseKerberos, StaticSettings.PrimaryDC);

                UsersObject obj = powershell.GetUser(identity, StaticSettings.ExchangeVersion);

                return(obj);
            }
            catch (Exception ex)
            {
                this.logger.Error("Failed to retrieve user mailbox information " + identity, ex);
                ThrowEvent(AlertID.FAILED, ex.Message);
                return(null);
            }
            finally
            {
                if (powershell != null)
                {
                    powershell.Dispose();
                }
            }
        }
示例#5
0
        public void DisableMailbox(string userPrincipalName)
        {
            CPDatabase         database   = null;
            ExchangePowershell powershell = null;

            try
            {
                database = new CPDatabase();

                // Get the user from the database
                var foundUser = (from u in database.Users
                                 where u.UserPrincipalName == userPrincipalName
                                 select u).FirstOrDefault();

                if (foundUser == null)
                {
                    ThrowEvent(AlertID.FAILED, "Unable to find user " + userPrincipalName);
                }
                else
                {
                    this.logger.Debug("Found user " + foundUser.UserPrincipalName + " in the database. Continuing...");

                    if (foundUser.MailboxPlan < 1)
                    {
                        this.logger.Debug("User " + foundUser.UserPrincipalName + " does not have a mailbox. Skipping...");
                    }
                    else
                    {
                        powershell = new ExchangePowershell(StaticSettings.ExchangeURI, StaticSettings.Username, StaticSettings.DecryptedPassword, StaticSettings.ExchangeUseKerberos, StaticSettings.PrimaryDC);
                        powershell.DeleteMailbox(foundUser.UserPrincipalName);

                        int r = database.DisableMailbox(foundUser.UserPrincipalName);

                        this.logger.Debug("Returned a total of " + r.ToString() + " records when calling DisableMailbox stored procedure for " + foundUser.UserPrincipalName);
                    }
                }
            }
            catch (Exception ex)
            {
                this.logger.Debug("Error deleting mailbox for " + userPrincipalName, ex);
                ThrowEvent(AlertID.FAILED, ex.Message);
            }
            finally
            {
                if (powershell != null)
                {
                    powershell.Dispose();
                }

                if (database != null)
                {
                    database.Dispose();
                }
            }
        }
示例#6
0
        public void NewContact(string companyCode, MailContactObject mailContact)
        {
            ExchangePowershell powershell = null;
            CPDatabase         database   = null;

            try
            {
                // Get company distinguished name
                database = new CPDatabase();
                var dn = (from c in database.Companies
                          where !c.IsReseller
                          where c.CompanyCode == companyCode
                          select c.DistinguishedName).First();

                powershell = new ExchangePowershell(StaticSettings.ExchangeURI, StaticSettings.Username, StaticSettings.DecryptedPassword, StaticSettings.ExchangeUseKerberos, StaticSettings.PrimaryDC);
                string distinguishedName = powershell.NewContact(mailContact.DisplayName, mailContact.Email, mailContact.Hidden, companyCode, "OU=Exchange," + dn);

                // Add contact to database
                Contact newContact = new Contact();
                newContact.DisplayName       = mailContact.DisplayName;
                newContact.CompanyCode       = companyCode;
                newContact.DistinguishedName = distinguishedName;
                newContact.Email             = mailContact.Email;
                newContact.Hidden            = mailContact.Hidden;
                database.Contacts.Add(newContact);
                database.SaveChanges();
            }
            catch (Exception ex)
            {
                ThrowEvent(Base.Enumerations.AlertID.FAILED, ex.Message);
            }
            finally
            {
                if (database != null)
                {
                    database.Dispose();
                }

                if (powershell != null)
                {
                    powershell.Dispose();
                }
            }
        }
示例#7
0
        public void DeleteContact(string distinguishedName, string companyCode)
        {
            ExchangePowershell powershell = null;
            CPDatabase         database   = null;

            try
            {
                // Get company distinguished name
                database = new CPDatabase();
                var contact = (from c in database.Contacts
                               where c.DistinguishedName == distinguishedName
                               select c).First();

                powershell = new ExchangePowershell(StaticSettings.ExchangeURI, StaticSettings.Username, StaticSettings.DecryptedPassword, StaticSettings.ExchangeUseKerberos, StaticSettings.PrimaryDC);
                powershell.DeleteContact(distinguishedName);

                database.Contacts.Remove(contact);
                database.SaveChanges();
            }
            catch (Exception ex)
            {
                ThrowEvent(Base.Enumerations.AlertID.FAILED, ex.Message);
            }
            finally
            {
                if (database != null)
                {
                    database.Dispose();
                }

                if (powershell != null)
                {
                    powershell.Dispose();
                }
            }
        }
示例#8
0
        public void DeleteDomain(string domainName, string companyCode)
        {
            CPDatabase           database   = null;
            ADOrganizationalUnit adOrg      = null;
            ExchangePowershell   powershell = null;

            try
            {
                database = new CPDatabase();

                // Make sure no users groups or anything is using this domain
                var usersUsing = (from u in database.Users
                                  where (u.UserPrincipalName.EndsWith("@" + domainName) || u.Email.EndsWith("@" + domainName))
                                  select u).Count();

                if (usersUsing > 0)
                {
                    ThrowEvent(AlertID.FAILED, "The domain is in use " + domainName);
                }
                else
                {
                    // Make sure no groups are using this domain
                    var groupsUsing = (from g in database.DistributionGroups
                                       where g.Email.EndsWith("@" + domainName)
                                       select g).Count();

                    if (groupsUsing > 0)
                    {
                        ThrowEvent(AlertID.FAILED, "The domain is in use " + domainName);
                    }
                    else
                    {
                        // Since users & groups are not using this domain we can continue and remove it

                        // Get company distinguished name
                        var dn = (from d in database.Companies
                                  where !d.IsReseller
                                  where d.CompanyCode == companyCode
                                  select d.DistinguishedName).First();

                        // Delete domain from Active Directory
                        adOrg = new ADOrganizationalUnit(StaticSettings.Username, StaticSettings.DecryptedPassword, StaticSettings.PrimaryDC);
                        adOrg.RemoveDomain(dn, domainName);

                        // Get domain from SQL
                        var domain = (from d in database.Domains
                                      where d.Domain1 == domainName
                                      where d.CompanyCode == companyCode
                                      select d).First();

                        // Check if it was enabled for Exchange
                        if (domain.IsAcceptedDomain)
                        {
                            powershell = new ExchangePowershell(StaticSettings.ExchangeURI, StaticSettings.Username, StaticSettings.DecryptedPassword, StaticSettings.ExchangeUseKerberos, StaticSettings.PrimaryDC);
                            powershell.DeleteDomain(domain.Domain1);
                        }

                        database.Domains.Remove(domain);
                        database.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                this.logger.Error("Failed to remove domain " + domainName + " from company " + companyCode, ex);
                ThrowEvent(Base.Enumerations.AlertID.FAILED, ex.Message);
            }
            finally
            {
                if (powershell != null)
                {
                    powershell.Dispose();
                }

                if (database != null)
                {
                    database.Dispose();
                }
            }
        }
示例#9
0
        public void  UpdateDomain(string domainName, string companyCode, bool isDefault, bool isExchangeEnabled, DomainType domainType)
        {
            CPDatabase         database   = null;
            ExchangePowershell powershell = null;

            try
            {
                // Remove any whitespace characters at the beginning and end
                domainName = domainName.Trim();

                database = new CPDatabase();
                var defaultDomains = from d in database.Domains
                                     where d.CompanyCode == companyCode
                                     select d;

                foreach (Domain d in defaultDomains)
                {
                    if (d.Domain1.Equals(domainName, StringComparison.CurrentCultureIgnoreCase))
                    {
                        // This is the domain we are updating
                        d.IsDefault  = isDefault;
                        d.DomainType = (int)domainType;

                        // Check if it wasn't an Exchange domain and we are making it an Exchange domain
                        if (!d.IsAcceptedDomain && isExchangeEnabled)
                        {
                            // Create accepted domain
                            powershell = new ExchangePowershell(StaticSettings.ExchangeURI, StaticSettings.Username, StaticSettings.DecryptedPassword, StaticSettings.ExchangeUseKerberos, StaticSettings.PrimaryDC);
                            powershell.NewDomain(domainName, domainType);
                            d.IsAcceptedDomain = true;
                        }
                        else if (d.IsAcceptedDomain && !isExchangeEnabled)
                        {
                            // Delete accepted domain
                            powershell = new ExchangePowershell(StaticSettings.ExchangeURI, StaticSettings.Username, StaticSettings.DecryptedPassword, StaticSettings.ExchangeUseKerberos, StaticSettings.PrimaryDC);
                            powershell.DeleteDomain(domainName);
                            d.IsAcceptedDomain = false;
                        }
                        else if (d.IsAcceptedDomain && isExchangeEnabled)
                        {
                            // Update accepted domain
                            powershell = new ExchangePowershell(StaticSettings.ExchangeURI, StaticSettings.Username, StaticSettings.DecryptedPassword, StaticSettings.ExchangeUseKerberos, StaticSettings.PrimaryDC);
                            powershell.UpdateDomain(domainName, domainType);
                            d.IsAcceptedDomain = true;
                        }
                    }
                    else
                    {
                        if (isDefault)
                        {
                            d.IsDefault = false;
                        }
                    }
                }

                database.SaveChanges();
            }
            catch (Exception ex)
            {
                this.logger.Error("Failed to update domain " + domainName + " for company " + companyCode, ex);
                ThrowEvent(AlertID.FAILED, ex.Message);
            }
            finally
            {
                if (database != null)
                {
                    database.Dispose();
                }
            }
        }
示例#10
0
        public void AddDomain(string domainName, string companyCode, bool isDefault, bool isExchangeEnabled, DomainType domainType)
        {
            CPDatabase           database   = null;
            ADOrganizationalUnit adOrg      = null;
            ExchangePowershell   powershell = null;

            CloudPanelTransaction transaction = new CloudPanelTransaction();

            try
            {
                // Get company distinguished name
                database = new CPDatabase();
                var dn = (from d in database.Companies
                          where !d.IsReseller
                          where d.CompanyCode == companyCode
                          select d.DistinguishedName).First();

                // Remove any whitespace characters at the beginning and end
                domainName = domainName.Trim();

                // Check if domain is already in database
                bool alreadyExist = IsDomainInUse(domainName);
                if (alreadyExist)
                {
                    ThrowEvent(Base.Enumerations.AlertID.FAILED, "Domain already exists");
                }
                else
                {
                    // Add domain to Active Directory
                    adOrg = new ADOrganizationalUnit(StaticSettings.Username, StaticSettings.DecryptedPassword, StaticSettings.PrimaryDC);
                    adOrg.AddDomain(dn, domainName);
                    transaction.NewDomain(dn, domainName);

                    // If it is default we need to remove default from all others
                    if (isDefault)
                    {
                        var defaultDomains = from d in database.Domains
                                             where d.CompanyCode == companyCode
                                             select d;

                        foreach (Domain d in defaultDomains)
                        {
                            if (d.IsDefault)
                            {
                                d.IsDefault = false;
                            }
                        }
                    }

                    //
                    // Check if it is Exchange enabled
                    //
                    if (isExchangeEnabled)
                    {
                        powershell = new ExchangePowershell(StaticSettings.ExchangeURI, StaticSettings.Username, StaticSettings.DecryptedPassword, StaticSettings.ExchangeUseKerberos, StaticSettings.PrimaryDC);
                        powershell.NewDomain(domainName, domainType);
                        transaction.NewExchangeDomain(domainName);
                    }

                    // Add new domain
                    Domain newDomain = new Domain();
                    newDomain.IsDefault        = isDefault;
                    newDomain.CompanyCode      = companyCode;
                    newDomain.Domain1          = domainName;
                    newDomain.IsSubDomain      = false;
                    newDomain.IsAcceptedDomain = isExchangeEnabled;
                    newDomain.IsLyncDomain     = false;
                    newDomain.DomainType       = (int)domainType;
                    database.Domains.Add(newDomain);

                    // Save all changes
                    database.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                this.logger.Error("Failed to add domain " + domainName + " to company " + companyCode, ex);

                ThrowEvent(Base.Enumerations.AlertID.FAILED, ex.Message);

                // Rollback
                transaction.RollBack();
            }
            finally
            {
                if (powershell != null)
                {
                    powershell.Dispose();
                }

                if (database != null)
                {
                    database.Dispose();
                }
            }
        }
示例#11
0
        public void EnableExchange(string companyCode)
        {
            ExchangePowershell powershell = null;
            CPDatabase         database   = null;

            CloudPanelTransaction newTransaction = new CloudPanelTransaction();

            try
            {
                powershell = new ExchangePowershell(StaticSettings.ExchangeURI, StaticSettings.Username, StaticSettings.DecryptedPassword, StaticSettings.ExchangeUseKerberos, StaticSettings.PrimaryDC);

                // Enable Exchange objects for company
                powershell.NewGlobalAddressList(RecipientFilters.GetGALName(companyCode), RecipientFilters.GetGALFilter(companyCode));
                newTransaction.NewExchangeGAL(RecipientFilters.GetGALName(companyCode));

                powershell.NewAddressList(RecipientFilters.GetUsersName(companyCode), RecipientFilters.GetUsersFilter(companyCode));
                newTransaction.NewExchangeAddressList(RecipientFilters.GetUsersName(companyCode));

                powershell.NewAddressList(RecipientFilters.GetGroupsName(companyCode), RecipientFilters.GetGroupsFilter(companyCode));
                newTransaction.NewExchangeAddressList(RecipientFilters.GetGroupsName(companyCode));

                powershell.NewAddressList(RecipientFilters.GetContactsName(companyCode), RecipientFilters.GetContactsFilter(companyCode));
                newTransaction.NewExchangeAddressList(RecipientFilters.GetContactsName(companyCode));

                powershell.NewAddressList(RecipientFilters.GetRoomName(companyCode), RecipientFilters.GetRoomFilter(companyCode));
                newTransaction.NewExchangeAddressList(RecipientFilters.GetRoomName(companyCode));

                powershell.NewOfflineAddressBook(RecipientFilters.GetOALName(companyCode), RecipientFilters.GetGALName(companyCode), "AllUsers@ " + companyCode);
                newTransaction.NewExchangeOAB(RecipientFilters.GetOALName(companyCode));

                powershell.NewAddressBookPolicy(RecipientFilters.GetABPName(companyCode), RecipientFilters.GetGALName(companyCode), RecipientFilters.GetOALName(companyCode), RecipientFilters.GetRoomName(companyCode), RecipientFilters.GetABPAddressLists(companyCode));
                newTransaction.NewExchangeABP(RecipientFilters.GetABPName(companyCode));

                database = new CPDatabase();
                var dn = (from c in database.Companies
                          where !c.IsReseller
                          where c.CompanyCode == companyCode
                          select c).First();

                powershell.NewSecurityDistributionGroup("ExchangeSecurity@" + companyCode, "AllUsers@" + companyCode, companyCode, "OU=Exchange," + dn.DistinguishedName);
                newTransaction.NewExchangeGroup("ExchangeSecurity@" + companyCode);

                // Set Exchange is enabled
                dn.ExchEnabled   = true;
                dn.ExchPermFixed = true;

                database.SaveChanges();
            }
            catch (Exception ex)
            {
                this.logger.Error("Error disabling Exchange for company " + companyCode, ex);

                newTransaction.RollBack();

                ThrowEvent(Base.Enumerations.AlertID.FAILED, ex.Message);
            }
            finally
            {
                if (database != null)
                {
                    database.Dispose();
                }

                if (powershell != null)
                {
                    powershell.Dispose();
                }
            }
        }
示例#12
0
        public void CreateMailbox(UsersObject user)
        {
            CPDatabase         database   = null;
            ExchangePowershell powershell = null;

            CloudPanelTransaction transaction = new CloudPanelTransaction();

            try
            {
                database = new CPDatabase();

                // Get the user from the database
                var foundUser = (from u in database.Users
                                 where u.UserPrincipalName == user.UserPrincipalName
                                 select u).FirstOrDefault();

                powershell = new ExchangePowershell(StaticSettings.ExchangeURI, StaticSettings.Username, StaticSettings.DecryptedPassword, StaticSettings.ExchangeUseKerberos, StaticSettings.PrimaryDC);

                // Get the selected mailbox plan
                MailboxPlanObject mailboxPlan = GetMailboxPlan(user.MailboxPlan);

                // Create new mailbox and register transaction
                powershell.NewMailbox(user);
                transaction.NewMailbox(user.UserPrincipalName);

                // Update the mailbox values
                powershell.UpdateMailbox(user, mailboxPlan);
                powershell.UpdateCASMailbox(user, mailboxPlan);

                // Set litigation hold settings if enabled for litigation hold
                if (user.LitigationHoldEnabled)
                {
                    powershell.NewLitigationHold(user.UserPrincipalName, user.LitigationHoldComment, user.LitigationHoldUrl, user.LitigationHoldDuration);
                }

                // Set archive settings if enabled for archiving
                if (user.ArchivingEnabled && user.ArchivePlan > 0)
                {
                    powershell.NewArchiveMailbox(user);
                    // Set quota on archive
                }

                foundUser.Email           = user.PrimarySmtpAddress;
                foundUser.MailboxPlan     = user.MailboxPlan;
                foundUser.AdditionalMB    = user.SetMailboxSizeInMB - mailboxPlan.MailboxSizeInMB;
                foundUser.ExchArchivePlan = user.ArchivePlan;
                database.SaveChanges();
            }
            catch (Exception ex)
            {
                this.logger.Error("Error creating mailbox for " + user.UserPrincipalName, ex);
                ThrowEvent(AlertID.FAILED, ex.Message);

                transaction.RollBack();
            }
            finally
            {
                if (powershell != null)
                {
                    powershell.Dispose();
                }

                if (database != null)
                {
                    database.Dispose();
                }
            }
        }