Exchange自动化脚本

时间:2017-03-16 14:32:22

标签: powershell date datetime exchange-server

我尝试为Exchange编写一个powershell脚本,该脚本将查找特定邮箱在1个月内收到的电子邮件数量,并将这些对象总计通过电子邮件发送给特定的个人。我到目前为止:

$date = Get-Date -Format MM/dd/yyyy
$time = Get-Date -Format HH:mm:ss
$previousMonth = 

PS C:\> Get-MessageTrackingLog -Recipients:mailbox1.org -start "02/01/2017 00:00:00" -end "$date $time" -EventId "Receive" -ResultSize unlimited | Measure-Object
PS C:\> Get-MessageTrackingLog -Recipients:mailbox2.org -start "02/01/2017 00:00:00" -end "$date $time" -EventId "Receive" -ResultSize unlimited | Measure-Object
PS C:\> Get-MessageTrackingLog -Recipients:mailbox3.org -start "02/01/2017 00:00:00" -end "$date $time" -EventId "Receive" -ResultSize unlimited | Measure-Object
PS C:\> Get-MessageTrackingLog -Recipients:mailbox4.org -start "02/01/2017 00:00:00" -end "$date $time" -EventId "Receive" -ResultSize unlimited | Measure-Object
ps c:\> Send-MailMessage  -SmtpServer "server" -To "recipient email<recipient email>" -From "Monthly Mailbox Totals <email address>" -Subject "Monthly Mailbox Item Count"  -Body ""

我遇到的问题是:我无法弄清楚如何计算-start输入的日期范围,以便从当前日期和时间获得上个月的第1天,其次,我坚持如何将这个格式的总数格式化为发送的电子邮件正文。我希望这个脚本在每个月的第一天运行,如果这有帮助的话。

任何提示将不胜感激,提前谢谢。

1 个答案:

答案 0 :(得分:0)

System.DateTime具有AddX功能(天,小时,分钟等)。通过这些,您可以指定确切的时间范围。然后,您只需将Get-MessageTrackingLog的返回值相加,然后将变量的内容发送出去。

$StartDate = (Get-Date).AddDays(-14) #Get the last 2 Weeks
$EndDate = Get-Date
$i = 0
$ += (Get-MessageTrackingLog -Recipients "RecipientA" -Start $StartDate -End $EndDate -EventId "Receive" -ResultSize unlimited | measure).Count
$ += (Get-MessageTrackingLog -Recipients "RecipientB" -Start $StartDate -End $EndDate -EventId "Receive" -ResultSize unlimited | measure).Count
Send-MailMessage -SmtpServer "server" -To "recipient" -From "sender" -Subject "Mailcount" -Body "Mail count for this month is $i"

如果您需要自己灵活设置时间范围,可以使用类型加速器将日期字符串转换为DateTime

$StartDate = [DateTime]"3.10.17"