none
EWS for Emails Modified on a certain date RRS feed

  • Question

  • I'm unable to find a powershell command to report on how many emails were modified after a certain date. Does this have to be done by EWS? If so, does someone have a sample that can help? Thanks.
    Monday, August 1, 2011 8:26 PM

All replies

  • You can use EWS to do this what version of exchange are you using ?? A very generic thing that would work on 2007 up would be something like this using the EWS Managed API and powershell

    $DateModified = [datetime]::Now.AddYears(-1)
    $MailboxName = "mailbox@domain.com"
    
    $dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\1.1\Microsoft.Exchange.WebServices.dll"
    [void][Reflection.Assembly]::LoadFile($dllpath)
    $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2007_SP1)
    $service.Credentials = New-Object System.Net.NetworkCredential("mailbox@domain.com","password")
    
    $Sf = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsGreaterThan([Microsoft.Exchange.WebServices.Data.ItemSchema]::LastModifiedTime, $DateModified)
    
    $service.AutodiscoverUrl($MailboxName,{$true})
    $PR_FOLDER_TYPE = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(13825,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer);
    
    "Checking : " + $MailboxName 
    $folderidcnt = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot,$MailboxName)
    $fvFolderView = New-Object Microsoft.Exchange.WebServices.Data.FolderView(1000)
    $fvFolderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep;
    $sfSearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo($PR_FOLDER_TYPE,"1")
    $fiResult = $Service.FindFolders($folderidcnt,$sfSearchFilter,$fvFolderView)
    foreach($ffFolder in $fiResult.Folders){
      $ItemView = New-Object Microsoft.Exchange.WebServices.Data.ItemView(1000)
      "Processing : " + $ffFolder.displayName
      do{
    	$fiItems = $ffFolder.findItems($Sf,$ItemView)
    	
    	foreach($Item in $fiItems.Items){
    		$Item.Subject
    	}
    	$ItemView.offset += $fiFindItems.Items.Count
      }while($fiFindItems.MoreAvailable -eq $true)
    }
    
    
    

    Cheers
    Glen

     

     

    Tuesday, August 2, 2011 3:39 AM
  • Hi, Thanks for the quick reply.  We are running Exchange 2010 SP1 RU1. I will give the code snippet a shot. Thanks a bunch!
    Tuesday, August 2, 2011 1:18 PM
  • Exception calling "AutodiscoverUrl" with "2" argument(s): "The Autodiscover service couldn't be located."
    At C:\Scripts\get-ModifiedDate_EWS.ps1:11 char:25
    + $service.AutodiscoverUrl <<<< ($MailboxName,{$true})
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : DotNetMethodException

    Checking : mai@domain.com
    Exception calling "FindFolders" with "3" argument(s): "The Url property on the ExchangeService object must be set."
    At C:\Scripts\get-ModifiedDate_EWS.ps1:19 char:33
    + $fiResult = $Service.FindFolders <<<< ($folderidcnt,$sfSearchFilter,$fvFolderView)
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : DotNetMethodException
     
    Processing :
    You cannot call a method on a null-valued expression.
    At C:\Scripts\get-ModifiedDate_EWS.ps1:24 char:32
    +     $fiItems = $ffFolder.findItems <<<< ($Sf,$ItemView)
        + CategoryInfo          : InvalidOperation: (findItems:String) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull
     
    Tuesday, August 2, 2011 2:15 PM
  • If your running this code using the credentials of the user that has access to the mailbox you trying to access then i would delete or Remark the line

    $service.Credentials = New-Object System.Net.NetworkCredential("mailbox@domain.com","password")

    You can enable tracing by adding a line like

    $service.TraceEnabled = $true

    If autodiscover isn't setup or working in your org you can manually set the by using something like

    $uri=[system.URI] "https://casservername/ews/exchange.asmx"
    $service.Url = $uri

     

    Cheers
    Glen

    Wednesday, August 3, 2011 12:21 AM
  • Great script as always Glen!

     

    Just a week question: at the end of your script, instead of $fiFindItems should it be $fiItems? $fiFindItems is not initialized anywhere...

     

     

    Regards,

    Nuno


    http://LetsExchange.blogspot.com
    Friday, August 5, 2011 7:30 PM
  • Good point Nuno there are a couple of typos should have been

    $DateModified = [datetime]::Now.AddYears(-1)
    $MailboxName = "mailbox@domain.com"
    
    $dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\1.1\Microsoft.Exchange.WebServices.dll"
    [void][Reflection.Assembly]::LoadFile($dllpath)
    $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2007_SP1)
    $service.Credentials = New-Object System.Net.NetworkCredential("mailbox@domain.com","password")
    
    $Sf = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsGreaterThan([Microsoft.Exchange.WebServices.Data.ItemSchema]::LastModifiedTime, $DateModified)
    
    $service.AutodiscoverUrl($MailboxName,{$true})
    $PR_FOLDER_TYPE = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(13825,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer);
    
    "Checking : " + $MailboxName 
    $folderidcnt = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot,$MailboxName)
    $fvFolderView = New-Object Microsoft.Exchange.WebServices.Data.FolderView(1000)
    $fvFolderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep;
    $sfSearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo($PR_FOLDER_TYPE,"1")
    $fiResult = $Service.FindFolders($folderidcnt,$sfSearchFilter,$fvFolderView)
    foreach($ffFolder in $fiResult.Folders){
     $ItemView = New-Object Microsoft.Exchange.WebServices.Data.ItemView(1000)
     "Processing : " + $ffFolder.displayName
    $fiItems = $null
     do{
    	$fiItems = $ffFolder.findItems($Sf,$ItemView)
    	
    	foreach($Item in $fiItems.Items){
    		$Item.Subject
    	}
    	$ItemView.offset += $fiItems.Items.Count
     }while($fiItems.MoreAvailable -eq $true)
    }
    

    Cheers
    Glen

    • Proposed as answer by Nuno Mota Thursday, September 8, 2011 12:55 PM
    Friday, August 5, 2011 11:11 PM