通过Powershell调研EWS API删除特定主题邮件操作手册
2021-03-28 20:26
标签:strong mit imp 查找 logfile odi 文件中 pow like 1. 说明 通过EWS API去删除特定主题邮件方法,比传统的Search-Mailbox去删除特定邮件的方法更有效。EWS去查找邮箱项目一次可以返回1000个对象(Exchange 2013可以通过策略解除限制,如果是Exchange 2016无法通过测量解除限制,如果某个邮箱中同一主题邮件超过1000时,需要多次执行脚本来删除邮件),而Search-mailbox一次检索只能返回250个对象。并且Search-mailbox查询无法精确匹配,有时候会将筛选条件无关的内容查询出来。 2、下载EWS manged API 2.2 首先通过如下地址下载Exchange EWS managed API。 3 安装EWS Manged API 2.2 4 为执行账号添加权限 在通过EWS进行邮件删除之前需要为当前执行账号添加一个ApplicationImpersonation角色权限。 5 创建一个限制策略取消EWS的限制 New-ThrottlingPolicy Ews_unlimited 6 调整EWS删除邮件执行脚本 7 执行批量删除指定主题列表邮件 将上面脚本保存为.ps1脚本【此示例中保存为ews01.ps1】。 其中CSV文件内容格式如下: SubjectList.csv格式如下 8 执行效果 通过Powershell调研EWS API删除特定主题邮件操作手册 标签:strong mit imp 查找 logfile odi 文件中 pow like 原文地址:http://blog.51cto.com/jialt/2145802
具体的操作过程如下:
EWS API可以在非Exchange上的任何服务器上执行,而Search-Mailbox命令只能在安装了Exchange Powershell工具的加域计算机上执行。
下载并安装EWS Managed API: https://www.microsoft.com/en-us/download/confirmation.aspx?id=42951
设置安装路径。
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 "
将下面标黄部分根据实际情况进行调整。#========================脚本开始=========================
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"
}
}
}
#===============================脚本结束====================================================
接下来,创建一个Action_Ews.ps1脚本,去调用ews01.ps1脚本,这样当执行Action_Ews.ps1脚本执行完成后,会在当前目录下产生一个delet_log文本文件,该文件中记录删除的邮件信息。(发件人、收件人、邮件主题和邮件接收时间)
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 "admin@itservice.vip" -password " P@ssw0rd"
}
}
#==================================脚本结束================================================
allMailboxList.csv 格式如下
上一篇:深入c#的string类
文章标题:通过Powershell调研EWS API删除特定主题邮件操作手册
文章链接:http://soscw.com/index.php/essay/69203.html