須要統計Exchange服務器在給定時間範圍(perWeek 7天,perMonth 30天…perSeason 90天等)收發郵件的總數和總容量大小,以便作成報表提出升級預期。c++
解決:統計總數很簡單,用命令帶上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上找到了這麼一則腳本ide
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.spa
直接拿來加以改動,獲得符合咱們需求的腳本,代碼以下:日誌
# 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))
原有的代碼也都保留下來,只是註釋掉了。orm
因爲咱們要統計今天以前30天的,那麼start就是30天以前的日期,to就是今天的日期。ip
(開始覺得只有增長天數的方法AddDays(),沒想到在裏頭填個負數就變成了減小天數的方法了)get
若是須要統計更多天數的,則直接改adddays()裏頭的數量。前提是你的MessageTrackingLog保留的時間足夠長。it
運行代碼後得到的結果以下:io
配置TrackingLog的方法:http://technet.microsoft.com/en-us/library/aa997984(v=exchg.150).aspx
當前的服務器我已經修改成保留90天,最大日誌總量1.5GB