none
EWS Powershell display all messages from Entire mailbox including all folders and their subfolders RRS feed

  • Question

  • Hi all,

    I am trying to get all IPM.note ( all msgs) from entire mailbox including all folders and their subfolders.

    I am getting an error on the line $fiItems = $fol.FindItems($fol.id,$searchFilterCollection,$ivItemView) 

    Cannot find an overload for "FindItems" and the argument count: "3".
    At line:14 char:6
    +      $fiItems = $fol.FindItems($fol.id,$searchFilterCollection,$ivIte ...
    +      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodException
        + FullyQualifiedErrorId : MethodCountCouldNotFindBest

    how do I fix the following script so that I can generate all msgs from entire Mailbox including all folders and their sub folders.

    Script is a below 

    # Script start

    Import-Module -Name "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"
    $userEmail = "testuser@testdomain.org"
    $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2013_SP1)
    $service.UseDefaultCredentials = $true
    $service.AutoDiscoverUrl($userEmail)
    $view = New-Object Microsoft.Exchange.WebServices.Data.FolderView(100)
    $view.PropertySet = New-Object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.Webservices.Data.BasePropertySet]::FirstClassProperties)
    $view.PropertySet.Add([Microsoft.Exchange.Webservices.Data.FolderSchema]::DisplayName)
    $view.Traversal = [Microsoft.Exchange.Webservices.Data.FolderTraversal]::Deep
    $findResults = $service.FindFolders([Microsoft.Exchange.Webservices.Data.WellKnownFolderName]::MsgFolderRoot, $view)
    foreach ($fol in $findResults)
    {
    $ivItemView =  New-Object Microsoft.Exchange.WebServices.Data.ItemView(1000)  
    $fiItems = $null  
    $searchFilterCollection = New-Object Microsoft.Exchange.WebServices.Data.SearchFilter+SearchFilterCollection([Microsoft.Exchange.WebServices.Data.LogicalOperator]::or)
    $searchFilter1 = New-Object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.ContactSchema]::ItemClass,"IPM.Note")
    $searchFilterCollection.add($searchFilter1)
    $return_object =@()
    do
    {  
         $fiItems = $fol.FindItems($fol.id,$searchFilterCollection,$ivItemView) 
      write-host "Folder-processing $($fol.displayname)"
        foreach($Item in $fiItems.Items){  
         $props = @{   RecivedDate =  $Item.DateTimeReceived   
            Subject =  $Item.Subject   
            Size    = $Item.Size  
    }
    $return_object += New-Object -TypeName PSCustomObject -Property $props
        }  
        $ivItemView.Offset += $fiItems.Items.Count  
    }while($fiItems.MoreAvailable -eq $true)  
    }
    # Script End

    Thursday, July 4, 2019 10:50 PM

Answers

  • Thank you so much Glen for your suggestion.

    I could able to display all emails including folders and every subfolders 

    The basic script is as below (please modify if need impersonation, dump to CSV, exclude any folders such as deleted items, add full folder path, Calendar, contacts etc )

    # Script Start Here

    Import-Module -Name "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"
    $userEmail = "testuser@testdomain.org"
    $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2013_SP1)
    $service.UseDefaultCredentials = $true
    $service.AutoDiscoverUrl($userEmail)
    $view = New-Object Microsoft.Exchange.WebServices.Data.FolderView(100)
    $view.PropertySet = New-Object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.Webservices.Data.BasePropertySet]::FirstClassProperties)
    $view.PropertySet.Add([Microsoft.Exchange.Webservices.Data.FolderSchema]::DisplayName)
    $view.Traversal = [Microsoft.Exchange.Webservices.Data.FolderTraversal]::Deep
    $findResults = $service.FindFolders([Microsoft.Exchange.Webservices.Data.WellKnownFolderName]::MsgFolderRoot, $view)
    foreach ($fol in $findResults)
    {
    $ivItemView =  New-Object Microsoft.Exchange.WebServices.Data.ItemView(1000) 
    $fiItems = $null 
    $searchFilterCollection = New-Object Microsoft.Exchange.WebServices.Data.SearchFilter+SearchFilterCollection([Microsoft.Exchange.WebServices.Data.LogicalOperator]::or)
    $searchFilter1 = New-Object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.ContactSchema]::ItemClass,"IPM.Note")
    $searchFilterCollection.add($searchFilter1)

    $fiItems = $fol.FindItems($searchFilter1,$ivItemView)
    do
    {
     $return_object =@()
     foreach($Item in $fiItems.Items){ 
     
         $props = @{   RecivedDate =  $Item.DateTimeReceived  
            Subject =  $Item.Subject  
            Size    = $Item.Size 
    }
    $return_object += New-Object -TypeName PSCustomObject -Property $props
        } 
        $ivItemView.Offset += $fiItems.Items.Count 
    }while($fiItems.MoreAvailable -eq $true) 
     write-host "Folder Processing $($fol.DisplayName)"
    $return_object | ft -autosize
    }

    # Script End Here

    Saturday, July 6, 2019 5:40 AM

All replies

  • because you calling the FindItem method on the Folder Object itself you don't need to pass in the Id of the folder so 

     $fol.FindItems($fol.id,$searchFilterCollection,$ivItemView) 

    should be

     $fol.FindItems($searchFilterCollection,$ivItemView) 

    Cheers
    Glen

    Friday, July 5, 2019 12:07 AM
  • Hi Glen, Thank you so much for reply

    I have tried and change to $fiItems = $fol.FindItems($searchFilterCollection,$ivItemView) 

    But I still unable to display any messages from any folders

    The output snippet looks liek as below

    Folder-processing Deleted Items
    Folder-processing Drafts
    Folder-processing IMP
    Folder-processing Inbox
    Folder-processing 2018
    Folder-processing DEC
    Folder-processing NOV
    Folder-processing Findme
    Folder-processing OCT
    Folder-processing 2019
    Folder-processing FEB
    Folder-processing JAN
    Folder-processing MARCH

    There are messages ( IPM.note)  in folders (deleted items, inbox, findme, OCT etc...)

    How can I display these messages?

    Thank you in advanced.

    Friday, July 5, 2019 12:57 AM
  • Thank you so much Glen for your suggestion.

    I could able to display all emails including folders and every subfolders 

    The basic script is as below (please modify if need impersonation, dump to CSV, exclude any folders such as deleted items, add full folder path, Calendar, contacts etc )

    # Script Start Here

    Import-Module -Name "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"
    $userEmail = "testuser@testdomain.org"
    $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2013_SP1)
    $service.UseDefaultCredentials = $true
    $service.AutoDiscoverUrl($userEmail)
    $view = New-Object Microsoft.Exchange.WebServices.Data.FolderView(100)
    $view.PropertySet = New-Object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.Webservices.Data.BasePropertySet]::FirstClassProperties)
    $view.PropertySet.Add([Microsoft.Exchange.Webservices.Data.FolderSchema]::DisplayName)
    $view.Traversal = [Microsoft.Exchange.Webservices.Data.FolderTraversal]::Deep
    $findResults = $service.FindFolders([Microsoft.Exchange.Webservices.Data.WellKnownFolderName]::MsgFolderRoot, $view)
    foreach ($fol in $findResults)
    {
    $ivItemView =  New-Object Microsoft.Exchange.WebServices.Data.ItemView(1000) 
    $fiItems = $null 
    $searchFilterCollection = New-Object Microsoft.Exchange.WebServices.Data.SearchFilter+SearchFilterCollection([Microsoft.Exchange.WebServices.Data.LogicalOperator]::or)
    $searchFilter1 = New-Object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.ContactSchema]::ItemClass,"IPM.Note")
    $searchFilterCollection.add($searchFilter1)

    $fiItems = $fol.FindItems($searchFilter1,$ivItemView)
    do
    {
     $return_object =@()
     foreach($Item in $fiItems.Items){ 
     
         $props = @{   RecivedDate =  $Item.DateTimeReceived  
            Subject =  $Item.Subject  
            Size    = $Item.Size 
    }
    $return_object += New-Object -TypeName PSCustomObject -Property $props
        } 
        $ivItemView.Offset += $fiItems.Items.Count 
    }while($fiItems.MoreAvailable -eq $true) 
     write-host "Folder Processing $($fol.DisplayName)"
    $return_object | ft -autosize
    }

    # Script End Here

    Saturday, July 6, 2019 5:40 AM