场景:需要统计Exchange服务器在给定时间范围(perWeek 7天,perMonth 30天…perSeason 90天等)收发邮件的总数和总容量大小,以便做成报表提出升级预期。
解决:统计总数很简单,用命令带上Meansure就可以输出count数量。
#接收就写Receive 发送就写Send Get-TransportServer | get-messagetrackinglog -Start "2012/4/1 10:42:00" -End "2012/5/1 00:00:00" -EventId Receive | Measure-object
但是如果要统计容量,就必须取变量,做加权了。在TechNet Gallery上找到了这么一则脚本
http://gallery.technet.microsoft.com/scriptcenter/ba5bd64d-6025-415a-86ed-2b51f9aabaf0
作用是:This script counts the number of e-mails that are sent and received in your Exchange environment per week, including their total size in MB.
直接拿来加以改动,得到符合我们需求的脚本,代码如下:
# Script: TotalEmailsSentReceivedPerMonth.ps1 # Purpose: Get the number of e-mails sent and received per week # Modified Purpose: get the number of e-mails sent and received per 30 days since today before. # Author: Nuno Mota # Date: October 2010 # Modify: Soda # ModifyDate: June 2014 # From date should be a MONDAY #$From = Get-Date "06/02/2012" #$To = $From.AddDays(7) #the $To date initializes as today‘s date, the $To date minus 30 days gives the $From date. $From = $to.adddays(-30) $to = get-date [Int64] $intTotalSentSize = $intTotalSent = 0 [Int64] $intTotalRecSize = $intTotalRec = 0 Write-Host "From, To, # Sent, Size Sent, # Received, Size Received" #Do #{ #Get-ExchangeServer | ? {$_.AdminDisplayVersion -match "8.3" -and $_.IsHubTransportServer -eq $True} | Get-MessageTrackingLog -ResultSize Unlimited -Start $FromSmall -End $ToSmall | ForEach { Get-TransportServer | Get-MessageTrackingLog -ResultSize Unlimited -Start $From -End $To | ? {$_.MessageSubject -ne "Folder Content"} | ForEach { # Sent E-mails If ($_.EventId -eq "RECEIVE" -and $_.Source -eq "STOREDRIVER") { $intTotalSentSize += $_.TotalBytes $intTotalSent++ } # Received E-mails If ($_.EventId -eq "DELIVER") { $intTotalRecSize += $_.TotalBytes $intTotalRec++ } } # Convert the size to MB and round it $intTotalSentSize = [Math]::Round($intTotalSentSize/1MB, 0) $intTotalRecSize = [Math]::Round($intTotalRecSize/1MB, 0) # Create a TempTo variable as when we are searching the logs we search up to the next day, but we want to print the day before $TempTo = ($To.AddDays(-1)).ToShortDateString() $FromSmall = $From.ToShortDateString() Write-Host "$FromSmall, $TempTo, $intTotalSent, $intTotalSentSize, $intTotalRec, $intTotalRecSize" # Reset the variables to do another search #$From = $From.AddDays(7) #$To = $From.AddDays(7) #$intTotalSentSize = $intTotalSent = 0 #$intTotalRecSize = $intTotalRec = 0 #} #While ($To -lt (Get-Date))
原有的代码也都保留下来,只是注释掉了。
由于我们要统计今天之前30天的,那么start就是30天之前的日期,to就是今天的日期。
(开始以为只有增加天数的方法AddDays(),没想到在里头填个负数就变成了减少天数的方法了)
如果需要统计更多天数的,则直接改adddays()里头的数量。前提是你的MessageTrackingLog保留的时间足够长。
运行代码后获得的结果如下:
配置TrackingLog的方法:http://technet.microsoft.com/en-us/library/aa997984(v=exchg.150).aspx
当前的服务器我已经修改为保留90天,最大日志总量1.5GB