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(); } }
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(); } }
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(); } }
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(); } }
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(); } }
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(); } }
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(); } }
private void Delete_OfflineAddressBook(string name) { ExchangePowershell powershell = null; try { this.logger.Warn("Rolling back action... Deleting address list " + name); powershell = new ExchangePowershell(StaticSettings.ExchangeURI, StaticSettings.Username, StaticSettings.DecryptedPassword, StaticSettings.ExchangeUseKerberos, StaticSettings.PrimaryDC); powershell.DeleteOfflineAddressBook(name); this.logger.Warn("Successfully removed address list " + name); } catch (Exception ex) { this.logger.Error("Failed to roll back action... Deleting address list " + name, ex); } finally { if (powershell != null) powershell.Dispose(); } }
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(); } }