Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
586 views
in Technique[技术] by (71.8m points)

exchange server - 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 = "[email protected]"
$Pass = "password"
$User_Email = "[email protected]"

$credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminName, $Pass
$AdminPass = $credential.GetNetworkCredential().password
$dllpath = "C:Program FilesMicrosoftExchangeWeb Services2.2Microsoft.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)
question from:https://stackoverflow.com/questions/65848154/powershell-using-ews-to-search-for-email-which-is-not-in-the-inbox

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 = "[email protected]"
$Pass = "password"
$User_Email = "[email protected]"

$credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminName, $Pass
$AdminPass = $credential.GetNetworkCredential().password
$dllpath = "C:Program FilesMicrosoftExchangeWeb Services2.2Microsoft.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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...