每个Exchange管理员都会遇到监控每天的邮件队列的需求:每天发送了多少邮件,收到了多少邮件,又有多少邮件被策略和反垃圾拦截掉了,而每次在Exchange Management Shell窗口中查询又不便展示,为了每天能够更好的监控投递的邮件,这里写了一个 邮件投递报告的脚本,算是抛砖引玉

#添加Exchange管理单元
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
#定义管理凭据
$Password = "12345678" | ConvertTo-SecureString -asPlainText -Force
$Username = "Domain\User"
$Credential = New-Object System.Management.Automation.PSCredential($username,$password)
#定义生成的CSV路径
$File = "C:\Scripts\Exchange\$(Get-Date -Format 'yyyy-MM-dd').csv"
#筛选前一天0点到今天0点的投递日志并存入Deliver变量
$Deliver = Get-MailboxServer | Get-MessageTrackingLog -Start (Get-Date).Date.AddDays(-1) -End (Get-Date).Date -EventId Deliver -ResultSize unlimited
#选择特定属性并按照主题排序,并输出到CSV
$Deliver | Select-Object Source,Sender,{$_.Recipients},MessageSubject | Sort-Object MessageSubject -Descending | Export-Csv -Path $File -NoClobber -NoTypeInformation -Encoding UTF8
#发送邮件报告
Send-MailMessage -From [email protected] -To [email protected] -SmtpServer mail.domain.com -Credential $Credential -Subject "Exchange Daily Tracking Log" -Attachments $File
#移除CSV文件
Remove-Item $File