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(); } } }