Delete specific topic mail Operations Manual via PowerShell research EWS API

Source: Internet
Author: User

Today to share a previously done case, call the Exchange EWS API through PowerShell to go to the last specific topic message. "I tested the environment Exchange version for Exchange 2016"
The following procedures are described:

1. Description

The use of the EWS API to remove a specific topic message method is more effective than the traditional search-mailbox to remove a particular message. EWS to find mailbox items can return 1000 objects at a time (Exchange 2013 can be de-restricted by policy, if Exchange 2016 cannot be lifted by measurement, if the same subject message in a mailbox exceeds 1000, the script needs to be executed multiple times to delete the message). Instead, Search-mailbox can return only 250 objects at a time. and Search-mailbox queries do not match exactly, and sometimes content that is unrelated to the filter is queried.
The EWS API can be executed on any server that is not exchange, and the Search-mailbox command can only be performed on an additional domain computer that has the Exchange PowerShell tool installed.

2. Download EWS manged API 2.2

First download the Exchange EWS managed API from the following address.
Download and install EWS Managed api:https://www.microsoft.com/en-us/download/confirmation.aspx?id=42951

3 Installing the EWS manged API 2.2

Set the installation path.

4 Adding permissions for the execution account

You need to add a applicationimpersonation role permission for the current execution account before message deletion via EWS.

5 Create a throttling policy to suppress EWS restrictions

New-throttlingpolicy ews_unlimited
Set-throttlingpolicy ews_unlimited-rcamaxconcurrency unlimited-ewsmaxconcurrency unlimited-ewsmaxsubscriptions Unlimited-cpamaxconcurrency unlimited-ewscutoffbalance Unlimited-ewsmaxburst unlimited-ewsrechargerate Unlimited
Set-mailbox-identity test-throttlingpolicy "ews_unlimited"

6 Adjusting the EWS delete message execution script
The following yellow section will be adjusted according to the actual situation.

#======================== script starts =========================param ($Mailbox, $userName = $cred. UserName, $password = $cred. Getnetworkcredential (). password,[string] $subject) $uri =[system. URI] "Https://mbx01.itservice.vip/ews/exchange.asmx" #服务器EWS url$dllpath = "C:\Program files\microsoft\exchange\web Services\2.2\microsoft.exchange.webservices.dll "#安装的EWS API path import-module $dllpath # Set Exchange Version and connect to Exchange Server$exchservice = New-object Microsoft.Exchange.WebServices.Data.ExchangeService ([ MICROSOFT.EXCHANGE.WEBSERVICES.DATA.EXCHANGEVERSION]::EXCHANGE2016_CU7) $ExchangeVersion = [ Microsoft.exchange.webservices.data.exchangeversion]::exchange2016_cu7$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService ($ExchangeVersion) $service. Credentials = New-object microsoft.exchange.webservices.data.webcredentials-argumentlist $userName, $password $ Service.url = $uri $service. Impersonateduserid = New-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId ' ([MICROsoft. Exchange.webservices.data.connectingidtype]::smtpaddress, $Mailbox); # $Mailbox is the Mailbox id need to be searched # Getting Folders in the mailbox# can change to Folder view if there is more than $ folder in the Mailbox$folders = N Ew-object Microsoft.Exchange.WebServices.Data.FolderId ([Microsoft.Exchange.WebServices.Data.WellKnownFolderName ]::msgfolderroot, $Mailbox) $MailboxRoot =[microsoft.exchange.webservices.data.folder]::bind ($service, $folders) $ Folderlist = New-object Microsoft.Exchange.WebServices.Data.FolderView ($FolderList). Traversal = [ Microsoft.Exchange.WebServices.Data.FolderTraversal]::D eep$findfolderresults = $MailboxRoot. Findfolders ($ folderlist) # "sender," + "receivedrepresenting," + "Subject," + "datetimereceived" > $logfileforeach ($FDR in $ findfolderresults.folders) {$emailsInFolder = $fdr. FindItems (1000000) foreach ($individualEmail in $emailsInFolder) {if ($individualEmail. subject-like "* $subject *") {"$ ( $individualEmail. Sender), "+" $ ($individualEmail. Receivedrepresenting), "+" $ ($individualEmail. Subject), "+" $ ($individualEmail. datetimereceived) "| Out-file $logfile-append-encoding Utf8echo "successfully found the email with subject $ ($individualEmail. Subject) from $ Mailbox "$individualEmail. Delete ([Microsoft.exchange.webservices.data.deletemode]::harddelete) echo" successfully deleted the email with subject $ ($individualEmail. Subject) from $Mailbox "}}}#=============================== script end ===== ===============================================

7 to perform bulk deletion of a specified topic list message

The

Saves the above script as a. PS1 script, which is saved as EWS01.PS1 in this example.
Next, create a ACTION_EWS.PS1 script to invoke the Ews01.ps1 script so that when execution of the Action_ews.ps1 script completes, a delet_log text file is generated in the current directory that records the deleted message information. (sender, recipient, message subject, and message receive time) The

Action_ews.ps1 script reads as follows:
#======================= script starts ============================
$mailboxlist =import-csv-path. \allmailboxlist.csv #用户邮箱列表文件
[string] $logfile = ". \delete_log.txt"
" sender, "+" receivedrepresenting, "+" Subject, "+" datetimereceived "> $logfile
foreach ($mailboxs in $mailboxlist)
{
$subjectlist =import-csv-path. \subjectlist.csv #主题列表
foreach ($subject in $subjectlist)
{
Write-host "Now finding subject are $ ($subject. Subject) from $ ($mailboxs. primarysmtpaddress) ... "
&. \ews01.ps1-mailbox $mailboxs. Primarysmtpaddress-subject $subject. Subject-username "[email protected]"-password "[email protected]"
}
}
#================================== script ends ================================================

Where the CSV file content format is as follows:
The allmailboxlist.csv format is as follows

The subjectlist.csv format is as follows

8 Execution effect

Delete specific topic mail Operations Manual via PowerShell research EWS API

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.