I have the following Powershell script that I was using on our Exchange 2010 server prior to upgrading to 2013. The script basically gets the item count & size of messages in the Deleted Items and Junk E-mail folders that are over 30 days old which
should get cleaned out by the Retention Policy cleanup process.
#Load the dll
$WebServicesdll = "E:\Program Files\Microsoft\Exchange\Web Services\1.2\Microsoft.Exchange.WebServices.dll"
[void][Reflection.Assembly]::LoadFile($WebServicesdll)
$Service = New-object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP1)
#Connect to Exchange
$Service.Url = New-Object System.Uri("https://CFCUMX01/ews/exchange.asmx")
$results = ""
$Mailboxes = get-mailbox | where {$_.RecipientTypeDetails -eq 'UserMailbox' -and $_.RetentionPolicy -eq 'All Users Cleanup'} | select PrimarySmtpAddress
foreach ($Mailbox in $Mailboxes) {
#Who are we looking for? - Note we need full rights to connect to this mailbox
$MailboxName = $Mailbox.PrimarySmtpAddress.ToString()
$results += "$MailboxName`r`n"
#Hook up to the mailbox folder
$FolderId = New-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::JunkEmail,$MailboxName)
$Folder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($Service,$FolderId)
$date= (get-date).AddDays(-30)
$oldItems = New-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsLessThan([Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::DateTimeReceived, $date)
$page=1000
$offset=0
$ItemView = New-object Microsoft.Exchange.WebServices.Data.ItemView($page,$offset)
$FolderView = New-object Microsoft.Exchange.WebServices.Data.FolderView(10000)
$FolderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep
$size=0
$itemcount=0
$fi=$Folder.FindItems($oldItems,$ItemView)
$finum = $fi.TotalCount
$pagenum = [Math]::Ceiling($finum/$page)
for ($p=0; $p -lt $pagenum; $p++) {
$ItemView = New-object Microsoft.Exchange.WebServices.Data.ItemView($page,($p * $page))
$items=$Folder.FindItems($oldItems,$ItemView)
foreach($item in $items) {
$size += $item.Size
$itemcount++
}
}
$size = [Math]::Round($size/1048576,2)
$displayname = $Folder.DisplayName
$results += " $displayname -> $itemcount items, totaling $size MB`r`n"
$FolderId = New-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::DeletedItems,$MailboxName)
$Folder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($Service,$FolderId)
$size=0
$itemcount=0
$fi=$Folder.FindItems($oldItems,$ItemView)
$finum = $fi.TotalCount
$pagenum = [Math]::Ceiling($finum/$page)
for ($p=0; $p -lt $pagenum; $p++) {
$ItemView = New-object Microsoft.Exchange.WebServices.Data.ItemView($page,($p * $page))
$items=$Folder.FindItems($oldItems,$ItemView)
foreach($item in $items) {
$size += $item.Size
$itemcount++
}
}
$size = [Math]::Round($size/1048576,2)
$displayname = $Folder.DisplayName
$results += " $displayname -> $itemcount items, totaling $size MB`r`n"
$results += "`r`n"
}
write-host $results
Based on the information at
https://msdn.microsoft.com/en-us/library/dd633678(v=exchg.80).aspx, it would appear that Exchange 2013 has all of the features (and then some) of any of the Managed API installs, so I have modified the code to use the native Microsoft.Exchange.WebServices.dll
file in Exchange 2013. The three lines I modified in the 2013 script are:
$WebServicesdll = "E:\Program Files\Microsoft\Exchange Server\V15\Bin\Microsoft.Exchange.WebServices.dll"
$Service = New-object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2013_SP1)
$Service.Url = New-Object System.Uri("https://CCCUMX01/ews/exchange.asmx")
The problem is that the line that gets the Folder ID is returning a blank/null value on the 2013 server.
New-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::JunkEmail,"username@domain.com") | FL
Returns:
FolderName : JunkEmail
Mailbox : username@domain.com
UniqueId :
ChangeKey :
Because the UniqueId is missing, it causes the script to throw an error when it tries to bind to the folder: "The specified folder could not be found in the store."
Has anyone else seen this issue? I'm wondering if it is an issue with the EWS dll that is provided in Exchange 2013, or if Exchange 2013 is actually suppressing the Folder ID (maybe due to a permissions issue or something). I haven't
tried any of the Managed API dll's yet, mainly because I wasn't sure if installing one (e.g. EWS Managed API 2.0) would conflict with the existing one on the server.
Any feedback would be appreciated.
Thanks,
Eric