简体   繁体   中英

Powershell: Using EWS to search for email which is not in the Inbox

I am able to search and find an email if it exists in the INBOX. But how do I search if the Same email is in a different folder. I am traversing to the root and going down the list, but when I do the search it does not return anything. I received a few tips from https://docs.microsoft.com/en-us/archive/blogs/santhse/get-readstatus . It works for the most part, although there are creating a folder "TEMP-MSG-ID" in the code, and when running this code over 50,000+ users the folder creation breaks infrequently. So I am trying to find an approach that works correctly for all mailboxes. Here is my code:

 $MsgSubject = "Complete Language" $AdminName = "a34d4557@wyx.com" $Pass = "password" $User_Email = "hil@wyx.com" $credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminName, $Pass $AdminPass = $credential.GetNetworkCredential().password $dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll" [void][Reflection.Assembly]::LoadFile($dllpath) $service = new-object Microsoft.Exchange.WebServices.Data.ExchangeService $Service.Credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials($AdminName,$AdminPass) $TestUrlCallback = { param ([string] $url) if ($url -eq "https://autodiscover-s.outlook.com/autodiscover/autodiscover.xml") {$true} else {$false} } $service.AutodiscoverUrl($User_Email,$TestUrlCallback) $PropertySet = New-Object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties) $PropertySet.RequestedBodyType = [Microsoft.Exchange.WebServices.Data.BodyType]::Text $mb = New-Object Microsoft.Exchange.WebServices.Data.Mailbox($User_Email) $InboxFolder = New-Object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox, $mb) #--------------------------------------------------- # ----- If email is in Inbox -- Works correctly ---- #--------------------------------------------------- $numOfEmailsToRead = 100 $index = 0 $view = New-Object Microsoft.Exchange.WebServices.Data.ItemView($numOfEmailsToRead,$index) $search1 = New-Object Microsoft.Exchange.WebServices.Data.SearchFilter+ContainsSubstring([Microsoft.Exchange.WebServices.Data.ItemSchema]::Subject, $MsgSubject, [Microsoft.Exchange.WebServices.Data.ContainmentMode]::Substring, [Microsoft.Exchange.WebServices.Data.ComparisonMode]::IgnoreCase) $UserMsgRecd = $service.FindItems($InboxFolder,$search1,$view) #---------------------------------------------------------------------------------------------- # ----- If email is not in the Inbox but different folder -- Need to get this part to work ---- #---------------------------------------------------------------------------------------------- $RootFolder = New-Object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Root, $mb) $EWSParentFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$RootFolder) $FolderView = New-Object Microsoft.Exchange.WebServices.Data.FolderView(100) $FolderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep $MailboxFolderList = $EWSParentFolder.FindFolders($FolderView) $UserMsgRecd = $service.FindItems($EWSParentFolder,$search1,$FolderView)

I would suggest you use the AllItems Search folder which is what the /messages endpoint in the Graph uses. To use that in EWS you need to search for it first to find the FolderId and then you can use it in the FindItems Method eg

 $MsgSubject = "Complete Language" $AdminName = "a34d4557@wyx.com" $Pass = "password" $User_Email = "hil@wyx.com" $credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminName, $Pass $AdminPass = $credential.GetNetworkCredential().password $dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll" [void][Reflection.Assembly]::LoadFile($dllpath) $service = new-object Microsoft.Exchange.WebServices.Data.ExchangeService $Service.Credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials($AdminName,$AdminPass) $TestUrlCallback = { param ([string] $url) if ($url -eq "https://autodiscover-s.outlook.com/autodiscover/autodiscover.xml") {$true} else {$false} } $service.AutodiscoverUrl($User_Email,$TestUrlCallback) $PropertySet = New-Object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties) $PropertySet.RequestedBodyType = [Microsoft.Exchange.WebServices.Data.BodyType]::Text $mb = New-Object Microsoft.Exchange.WebServices.Data.Mailbox($User_Email) $folderidcnt = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Root, $mb) $fvFolderView = New-Object Microsoft.Exchange.WebServices.Data.FolderView(1) $fvFolderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Shallow; $SfSearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName, "allitems") $findFolderResults = $Service.FindFolders($folderidcnt, $SfSearchFilter, $fvFolderView) $numOfEmailsToRead = 100 $index = 0 $view = New-Object Microsoft.Exchange.WebServices.Data.ItemView($numOfEmailsToRead,$index) $search1 = New-Object Microsoft.Exchange.WebServices.Data.SearchFilter+ContainsSubstring([Microsoft.Exchange.WebServices.Data.ItemSchema]::Subject, $MsgSubject, [Microsoft.Exchange.WebServices.Data.ContainmentMode]::Substring, [Microsoft.Exchange.WebServices.Data.ComparisonMode]::IgnoreCase) $UserMsgRecd = $service.FindItems($findFolderResults.Folders[0],$search1,$view)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM