This is the third of my samples based on the EWS Managed API and Powershell how to series I've been writing so if your looking for background on this please refer to these other posts.
EWS has a number of good Synchronization operations which I'll be covering in future posts but i thought I'd go through another method you can use if your looking to find new Items or changed items in a Mailbox.
For example say you have a mailbox with a complicated folder structure with a number of Inbox rules that moves messages to different Folders and you want a script that that will look at all the folders in a Mailbox and only query those folders where the Items in that folder have been updated say since yesterday and return information about those items.
The property you need to use on a folder to know if the underlying Items have change is the PR_LOCAL_COMMIT_TIME_MAX which is one the properties using by ICS see . So we can basically use this property within a Searchfilter in EWS to determine if the underlying items in a folder has been updated after a particular date. To work out which Items are new you then need a conventional AQS item query to look for Items that where received after that said time
There are a few options in this script the first is the time you look at which is controlled by this variable
$queryTime = [system.DateTime]::Now.AddDays(-1)
(this should be self explanatory in terms of date)
The other things I've done is this script is to also query the Dumpster V2 so if Single Item Recovery is enabled in a Mailbox it makes sure it captures everything that has entered. This is done by the following lines
##Option add in the Deletions Folder
$delfolderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::RecoverableItemsDeletions,$MailboxName)
$DelFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$delfolderid)
and
$findFolderResults.Folders.Add($DelFolder)
I've put a download of this script here the script itself looks like
EWS has a number of good Synchronization operations which I'll be covering in future posts but i thought I'd go through another method you can use if your looking to find new Items or changed items in a Mailbox.
For example say you have a mailbox with a complicated folder structure with a number of Inbox rules that moves messages to different Folders and you want a script that that will look at all the folders in a Mailbox and only query those folders where the Items in that folder have been updated say since yesterday and return information about those items.
The property you need to use on a folder to know if the underlying Items have change is the PR_LOCAL_COMMIT_TIME_MAX which is one the properties using by ICS see . So we can basically use this property within a Searchfilter in EWS to determine if the underlying items in a folder has been updated after a particular date. To work out which Items are new you then need a conventional AQS item query to look for Items that where received after that said time
There are a few options in this script the first is the time you look at which is controlled by this variable
$queryTime = [system.DateTime]::Now.AddDays(-1)
(this should be self explanatory in terms of date)
The other things I've done is this script is to also query the Dumpster V2 so if Single Item Recovery is enabled in a Mailbox it makes sure it captures everything that has entered. This is done by the following lines
##Option add in the Deletions Folder
$delfolderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::RecoverableItemsDeletions,$MailboxName)
$DelFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$delfolderid)
and
$findFolderResults.Folders.Add($DelFolder)
I've put a download of this script here the script itself looks like
- ## Load Managed API dll
- Add-Type -Path "C:\Program Files\Microsoft\Exchange\Web Services\1.1\Microsoft.Exchange.WebServices.dll"
- ## Set Exchange Version
- $ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP1
- ## Create Exchange Service Object
- $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)
- ## Set Credentials to use two options are availible Option1 to use explict credentials or Option 2 use the Default (logged On) credentials
- #Credentials Option 1 using UPN for the windows Account
- $creds = New-Object System.Net.NetworkCredential("user@domain.com","password")
- $service.Credentials = $creds
- #Credentials Option 2
- #service.UseDefaultCredentials = $true
- ## Choose to ignore any SSL Warning issues caused by Self Signed Certificates
- [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
- ## Set the URL of the CAS (Client Access Server) to use two options are availbe to use Autodiscover to find the CAS URL or Hardcode the CAS to use
- $MailboxName = "user@domain.com"
- #CAS URL Option 1 Autodiscover
- $service.AutodiscoverUrl($MailboxName,{$true})
- "Using CAS Server : " + $Service.url
- #Define Query Time
- $queryTime = [system.DateTime]::Now.AddDays(-1)
- $PR_LOCAL_COMMIT_TIME_MAX = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x670A, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::SystemTime);
- $fvFolderView = new-object Microsoft.Exchange.WebServices.Data.FolderView(1000)
- $fvFolderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep
- $folderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot,$MailboxName)
- $SfSearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsGreaterThan($PR_LOCAL_COMMIT_TIME_MAX,$queryTime)
- ##Option add in the Deletions Folder
- $delfolderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::RecoverableItemsDeletions,$MailboxName)
- $DelFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$delfolderid)
- $tfTargetFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
- $findFolderResults = $tfTargetFolder.FindFolders($SfSearchFilter,$fvFolderView)
- $findFolderResults.Folders.Add($DelFolder)
- $AQSString = "System.Message.DateReceived:>" + $queryTime.ToString("MM/dd/yyyy")
- $AQSString
- foreach($folder in $findFolderResults.Folders){
- if($folder.TotalCount -gt 0){
- "Processing Folder " + $folder.DisplayName
- $ivItemView = New-Object Microsoft.Exchange.WebServices.Data.ItemView(1000)
- $findItemsResults = $null
- do{
- $findItemsResults = $folder.FindItems($AQSString,$ivItemView)
- foreach($itItem in $findItemsResults.Items){
- $itItem.Subject
- }
- $ivItemView.offset += $findItemsResults.Items.Count
- }while($findItemsResults.MoreAvailable -eq $true)
- }
- }