通過Powershell調研EWS API刪除特定主題郵件操作手冊

今天給大家分享一個之前做過的案例,通過Powershell調用Exchange ews API去刪除特定主題郵件。【我測試的環境Exchange版本爲Exchange 2016】
具體的操作過程如下:

1. 說明

通過EWS API去刪除特定主題郵件方法,比傳統的Search-Mailbox去刪除特定郵件的方法更有效。EWS去查找郵箱項目一次可以返回1000個對象(Exchange 2013可以通過策略解除限制,如果是Exchange 2016無法通過測量解除限制,如果某個郵箱中同一主題郵件超過1000時,需要多次執行腳本來刪除郵件),而Search-mailbox一次檢索只能返回250個對象。並且Search-mailbox查詢無法精確匹配,有時候會將篩選條件無關的內容查詢出來。
EWS API可以在非Exchange上的任何服務器上執行,而Search-Mailbox命令只能在安裝了Exchange Powershell工具的加域計算機上執行。

2、下載EWS manged API 2.2

首先通過如下地址下載Exchange EWS managed API。
下載並安裝EWS Managed API: https://www.microsoft.com/en-us/download/confirmation.aspx?id=42951

3 安裝EWS Manged API 2.2
通過Powershell調研EWS API刪除特定主題郵件操作手冊
設置安裝路徑。
通過Powershell調研EWS API刪除特定主題郵件操作手冊
通過Powershell調研EWS API刪除特定主題郵件操作手冊

4 爲執行賬號添加權限

在通過EWS進行郵件刪除之前需要爲當前執行賬號添加一個ApplicationImpersonation角色權限。
通過Powershell調研EWS API刪除特定主題郵件操作手冊

5 創建一個限制策略取消EWS的限制

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 "
通過Powershell調研EWS API刪除特定主題郵件操作手冊

6 調整EWS刪除郵件執行腳本
將下面標黃部分根據實際情況進行調整。

#========================腳本開始=========================
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路徑
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
# you can change to folder view if there are more than 100 folder in the mailbox
$folders = New-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(100)
$FolderList.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep
$findFolderResults = $MailboxRoot.FindFolders($FolderList)
 #"sender,"+"ReceivedRepresenting,"+"Subject,"+"DateTimeReceived" > $logfile
foreach ($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 utf8
echo "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"
}
}
}
#===============================腳本結束====================================================

7 執行批量刪除指定主題列表郵件

將上面腳本保存爲.ps1腳本【此示例中保存爲ews01.ps1】。
接下來,創建一個Action_Ews.ps1腳本,去調用ews01.ps1腳本,這樣當執行Action_Ews.ps1腳本執行完成後,會在當前目錄下產生一個delet_log文本文件,該文件中記錄刪除的郵件信息。(發件人、收件人、郵件主題和郵件接收時間)
通過Powershell調研EWS API刪除特定主題郵件操作手冊
Action_Ews.ps1腳本內容如下:

#=======================腳本開始============================
$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 is $($subject.subject) from $($mailboxs.PrimarySmtpAddress)........."
       & .\ews01.ps1 -Mailbox $mailboxs.PrimarySmtpAddress -subject $subject.subject -userName "[email protected]" -password " P@ssw0rd"
     }
} 
#==================================腳本結束================================================

其中CSV文件內容格式如下:
allMailboxList.csv 格式如下
通過Powershell調研EWS API刪除特定主題郵件操作手冊

SubjectList.csv格式如下
通過Powershell調研EWS API刪除特定主題郵件操作手冊

8 執行效果

通過Powershell調研EWS API刪除特定主題郵件操作手冊

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章