Udostępnij za pośrednictwem


Empty your online archives with EWS Managed API

In case you want to remove the content of your online archives, I have written this script that does it automatically for you.

 

Prerequisites:
-Create a file with the user mailboxes that have an online archive enabled: “UserAccounts.txt”
EmailAddress
user1@domain.onmicrosoft.com
user2@domain.onmicrosoft.com
user3@domain.onmicrosoft.com

-Create a new RBAC group or use an existing one from Exchange Admin Center – Permissions – Admin Roles. Add the ‘ApplicationImpersonation’ role to the group and add as member the service account that will impersonate the mailboxes from "UserAccounts.txt"

-The script requires EWS Managed API 2.2, which can be downloaded here: https://www.microsoft.com/en-gb/download/details.aspx?id=42951

-You need to be connected to Exchange Online when you run the script (copy/paste it into a Notepad file and save it with the extension ".ps1"): https://technet.microsoft.com/en-us/library/jj984289(v=exchg.160).aspx

-Create a log file under "C:\Temp\Log.txt"

 

 

 

 DISCLAIMER: This application is a sample application. The sample is provided "as is" without warranty of any kind. Microsoft further disclaims all implied warranties including without limitation any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the samples remains with you. in no event shall Microsoft or its suppliers be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss arising out of the use of or inability to use the samples, even if Microsoft has been advised of the possibility of such damages). Because some states do not allow the exclusion or limitation of liability for consequential or incidental damages, the above limitation may not apply to you.



[string]$LogFile = "C:\Temp\Log.txt" 
$ErrorActionPreference = "Stop"


Import-Module -Name "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"

$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService -ArgumentList Exchange2013_SP1

#Provide the credentials of the O365 account that has impersonation rights on the mailboxes declared in UserAccounts.txt
$credential = Get-Credential -Message "Provide the credentials of the O365 account that has impersonation rights"
$service.Credentials = new-object Microsoft.Exchange.WebServices.Data.WebCredentials -ArgumentList $credential.UserName, $credential.GetNetworkCredential().Password

#Exchange Online URL
$service.Url= new-object Uri("https://outlook.office365.com/EWS/Exchange.asmx")


function DeleteArchiveContent()
{

Write-Host "User $($MailboxName):"

Add-Content $LogFile ("User $($MailboxName):")

#Change the user to impersonate
$service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress,$MailboxName)

if ((Get-Mailbox $MailboxName).ArchiveStatus -match 'Active')

{

do
{

$AFolderView = new-object Microsoft.Exchange.WebServices.Data.FolderView(100)

$AFolderView.Traversal = [Microsoft.Exchange.Webservices.Data.FolderTraversal]::Deep
 
$AFindFolderResults = $service.FindFolders([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::ArchiveMsgFolderRoot,$AFolderView)

if ($AFindFolderResults.TotalCount)
{

Write-host "We have found $($AFindFolderResults.TotalCount) folders in the Archive mailbox of the user $($MailboxName)" -ForegroundColor  White

Add-Content $LogFile ("We have found $($AFindFolderResults.TotalCount) folders in the Archive mailbox of the user $($MailboxName)")


$AFindFolderResults | % {


$AItemView = new-object Microsoft.Exchange.WebServices.Data.ItemView(100)

$AFindItemResults = $service.FindItems($_.Id,$AItemView)


 if($AFindItemResults.TotalCount) {

Write-Host "Emptying the folder $($_.DisplayName)..." -ForegroundColor White

Add-Content $LogFile ("Emptying the folder $($_.DisplayName)...")

$Folder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$_.Id)

$Folder.Empty([Microsoft.Exchange.WebServices.Data.DeleteMode]::SoftDelete,$null)

                                                                }

                       else { 
    
                       Write-Host "The folder $($_.DisplayName) is already empty" -ForegroundColor Yellow  
    
                       Add-Content $LogFile ("The folder $($_.DisplayName) is already empty")

                             }

                                              }
 }

  else { 
  
  Write-Host "There aren't any folders in the Archive mailbox of the user $($MailboxName)" -ForegroundColor Yellow 
  
  Add-Content $LogFile ("There aren't any folders in the Archive mailbox of the user $($MailboxName)")
   
        }


} while($AFindFolderResults.MoreAvailable -eq $true)

}

 else { 
 
 Write-Host "The user $($MailboxName) doesn't have an active online archive" -ForegroundColor Red 
 
 Add-Content $LogFile ("The user $($MailboxName) doesn't have an active online archive")

      }

}


#Read the data
import-csv "C:\Users\your_account\Desktop\UserAccounts.txt" | % {
$MailboxName = $_.EmailAddress.ToString()

trap [System.Exception] 
    {
        Write-host ("Error: " + $_.Exception.Message) -ForegroundColor Red
        Add-Content $LogFile ("Error: " + $_.Exception.Message)
        continue
    }

DeleteArchiveContent

}

 

Note: In case you want to hard delete the content of the archive folders, replace the line below:
$Folder.Empty([Microsoft.Exchange.WebServices.Data.DeleteMode]::SoftDelete,$null) with $Folder.Empty([Microsoft.Exchange.WebServices.Data.DeleteMode]::HardDelete,$null)