Hi All,
I'm trying to figure out how to search all items in Outlook mailbox using EWS. I have a script that currently search "Sent Items" only. See script below. I just need to modify it to search all folders instead. Any help is appreciated. Thank you
$Report = @() #Provide text files for all users in the legal department $Law = cat law.txt #Set Date to 1 Year Ago $Date = (Get-Date).AddYears(-1) #Logon to Exchange Web Service with default credentials Add-Type -Path "C:\Program Files\Microsoft\Exchange\Web Services\1.2\Microsoft.Exchange.WebServices.dll" $sid = [System.Security.Principal.WindowsIdentity]::GetCurrent().User.Value $user = [ADSI]"LDAP://<SID=$sid>" $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService -ArgumentList ([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2) $service.AutodiscoverUrl($user.Properties.mail) Write-Progress -Activity "Preparing" -Status "Retrieving mailbox list" -PercentComplete 0 #Get Mailboxes for all users in the text file $Mailboxes = $law | Get-User | Select WindowsEmailAddress, Company $Count = $Mailboxes.Count #Go through each users found and process accordingly ForEach ($Mailbox in $Mailboxes){ $DisplayName = $Mailbox.DisplayName $i = $i + 1 $pct = $i/$Count * 100 Write-Progress -Activity "Collecting mailbox details" -Status "Processing mailbox $i of $Count - $DisplayName" -PercentComplete $pct Try { $Ok = $true $Mailbox = (Get-Mailbox $mailbox.WindowsEmailAddress -ErrorAction Stop ).PrimarySMTPAddress} catch [System.Exception]{ $Ok = $false } if ($Ok){ #Set EWS up to impersonationate user $ImpersonatedUserId = New-Object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId -ArgumentList ([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress),$Mailbox $service.ImpersonatedUserId = $ImpersonatedUserId #Open user folder and bind SentItems folder to the EWS service. $folderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::SentItems,$Mailbox) $SentFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid) #Specify Search Filters: Specify Date and Message Class Type $Sfir = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsGreaterThanOrEqualTo([Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::DateTimeSent, $Date) $Sfir2 = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::ItemClass, "IPM.Note") #Add search filters together to form one search $sfCollection = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+SearchFilterCollection([Microsoft.Exchange.WebServices.Data.LogicalOperator]::AND); $sfCollection.Add($Sfir) $sfCollection.Add($Sfir2) #Create PropertySet to make it possible to retreive all properties of email content $psPropset = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties) $SentItemsview = new-object Microsoft.Exchange.WebServices.Data.ItemView(1000) $fiItems = $null #Loop through all all items in 1000 page increment until all items are processed Do { #Find Items based on folder Id, search filter and page view $fiItems = $Service.FindItems($SentFolder.Id,$sfCollection,$SentItemsView) #Create PropertySet to make it possible to retreive all properties of email content [Void]$service.LoadPropertiesForItems($fiItems,$psPropset) #Loop through each email item and retrieve recipients info. ForEach ($item in $fiItems) { $AllAttendees = $item.ToRecipients | Select -Expand Address $AllAttendees += $item.CCRecipients | Select -Expand Address $AllAttendees += $item.BCCRecipients | Select -Expand Address $sender = $item.From.Address $subject = $item.Subject $TimeSent = $item.DateTimeSent Write-Host "$Sender --- mailbox --- $TimeSent" for ($index = 0; $index -lt $AllAttendees.count; $index++) { Write-Progress -Activity "Looping" -Status "Going through all recipients list" -PercentComplete 0 $Attendees = $AllAttendees[$index] #Filter invalid users, external users and users in Legal department If ($Attendees -like "*domain.com"){ If ($Law -notcontains $Attendees){ $a = Get-User $Attendees -filter {Company -ne $null} -ErrorAction SilentlyContinue if ($a){ $Obj = New-Object -TypeName PSObject $Obj | Add-Member -MemberType NoteProperty -Name Subject -Value $subject $Obj | Add-Member -MemberType NoteProperty -Name Sender -Value $sender $Obj | Add-Member -MemberType NoteProperty -Name Sent -Value $TimeSent $Obj | Add-Member -MemberType NoteProperty -Name Recipients -Value $Attendees $Report += $Obj } } } } } $SentItemsView.Offset += $fiItems.Items.Count } While ($fiItems.MoreAvailable -eq $true) } } #Export report to CSV $Report | Export-Csv "C:\Users\user\Dropbox\Script\LawData.csv" -NoTypeInformation -Encoding UTF8
Tunde