From 0ecd34363a58ae7da51d8fc3086d61ad4b13f588 Mon Sep 17 00:00:00 2001 From: robvde Date: Fri, 10 Aug 2012 11:28:02 +0400 Subject: [PATCH] Fixed: When deleting an exchange user the operations failed with "Cannot delete object when there are leaf objects Ensure activesync devices are removed before deleting or disabling a mailbox --- .../Exchange2007.cs | 49 +++++++++++++++++++ .../Exchange2010SP2.cs | 5 ++ 2 files changed, 54 insertions(+) diff --git a/WebsitePanel/Sources/WebsitePanel.Providers.HostedSolution/Exchange2007.cs b/WebsitePanel/Sources/WebsitePanel.Providers.HostedSolution/Exchange2007.cs index 62f54b5d..e48e2dca 100644 --- a/WebsitePanel/Sources/WebsitePanel.Providers.HostedSolution/Exchange2007.cs +++ b/WebsitePanel/Sources/WebsitePanel.Providers.HostedSolution/Exchange2007.cs @@ -467,6 +467,9 @@ namespace WebsitePanel.Providers.HostedSolution try { runSpace = OpenRunspace(); + + RemoveDevicesInternal(runSpace, id); + Command cmd = new Command("Disable-Mailbox"); cmd.Parameters.Add("Identity", id); cmd.Parameters.Add("Confirm", false); @@ -1087,6 +1090,8 @@ namespace WebsitePanel.Providers.HostedSolution try { id = ObjToString(GetPSObjectProperty(obj, "Identity")); + RemoveDevicesInternal(runSpace, id); + RemoveMailbox(runSpace, id); } catch (Exception ex) @@ -2063,6 +2068,8 @@ namespace WebsitePanel.Providers.HostedSolution { runSpace = OpenRunspace(); + RemoveDevicesInternal(runSpace, accountName); + RemoveMailbox(runSpace, accountName); } finally @@ -6296,6 +6303,48 @@ namespace WebsitePanel.Providers.HostedSolution ExchangeLog.LogEnd("CancelRemoteWipeRequestInternal"); } + + internal void RemoveDevicesInternal(Runspace runSpace, string accountName) + { + ExchangeLog.LogStart("RemoveDevicesInternal"); + ExchangeLog.DebugInfo("Account name: {0}", accountName); + + try + { + runSpace = OpenRunspace(); + Command cmd = new Command("Get-ActiveSyncDeviceStatistics"); + cmd.Parameters.Add("Mailbox", accountName); + + Collection result = null; + try + { + result = ExecuteShellCommand(runSpace, cmd); + } + catch (Exception) + { + } + + if (result != null) + { + foreach (PSObject obj in result) + { + ExchangeMobileDevice device = GetMobileDeviceObject(obj); + + cmd = new Command("Remove-ActiveSyncDevice"); + cmd.Parameters.Add("Identity", device.DeviceID); + cmd.Parameters.Add("Confirm", false); + ExecuteShellCommand(runSpace, cmd); + } + } + } + finally + { + + } + ExchangeLog.LogEnd("RemoveDevicesInternal"); + } + + private void RemoveDeviceInternal(string id) { ExchangeLog.LogStart("RemoveDeviceInternal"); diff --git a/WebsitePanel/Sources/WebsitePanel.Providers.HostedSolution/Exchange2010SP2.cs b/WebsitePanel/Sources/WebsitePanel.Providers.HostedSolution/Exchange2010SP2.cs index ad331d58..65d4dabf 100644 --- a/WebsitePanel/Sources/WebsitePanel.Providers.HostedSolution/Exchange2010SP2.cs +++ b/WebsitePanel/Sources/WebsitePanel.Providers.HostedSolution/Exchange2010SP2.cs @@ -487,10 +487,13 @@ namespace WebsitePanel.Providers.HostedSolution string addressbookPolicy = ObjToString(GetPSObjectProperty(result[0], "AddressBookPolicy")); + RemoveDevicesInternal(runSpace, id); + cmd = new Command("Disable-Mailbox"); cmd.Parameters.Add("Identity", id); cmd.Parameters.Add("Confirm", false); ExecuteShellCommand(runSpace, cmd); + if (addressbookPolicy == (upn + " AP")) { @@ -552,6 +555,8 @@ namespace WebsitePanel.Providers.HostedSolution string upn = ObjToString(GetPSObjectProperty(result[0], "UserPrincipalName")); string addressbookPolicy = ObjToString(GetPSObjectProperty(result[0], "AddressBookPolicy")); + RemoveDevicesInternal(runSpace, accountName); + RemoveMailbox(runSpace, accountName); if (addressbookPolicy == (upn + " AP"))