private void Delete_Domain(string distinguishedName, string domainName) { ADOrganizationalUnit org = null; try { this.logger.Warn("Rolling back action... Deleting domain from " + distinguishedName); org = new ADOrganizationalUnit(StaticSettings.Username, StaticSettings.DecryptedPassword, StaticSettings.PrimaryDC); org.RemoveDomain(distinguishedName, domainName); this.logger.Warn("Successfully removed domain " + domainName + " from " + distinguishedName + " and all child OU's"); } catch (Exception ex) { this.logger.Error("Failed to roll back action... Deleting domain from " + distinguishedName, ex); } }
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(); } } }