Number of sent and received emails by day

there is a need to view on server exchange how many emails to send and get users for days with statistics in megabytes, after studying the Internet was found a script Nuno Mota did not accept that the script takes the text output that is not convenient for continuous use, and to generate html reports. The script has been reworked:

the
    the
  • conclusion objects
  • the
  • added fields
  • the
  • fixed a bug where the first entry was displayed incorrectly


the get number of sent and received emails with exchange server.

the
#### Variables #####
# the period in which we see the statistics, it is considered from today
$PeriodIndays = 7
# date at which you look, should be less than the starting
$EndPeriod = Get-date-hour 0-minute 0 -second 0
#  if  you write manually remember what the date is naobrot MM/DD/YYYY, ie "09/5/2016"
$StartPeriod = ($EndPeriod).AddDays( -$PeriodIndays )



####################################################################################
$From = $StartPeriod
$To = $EndPeriod

[Int64] $intSent = 0
[Int64] $intRec = 0
[Int64] $intSentSize = 0
[Int64] $intRecSize = 0
$Total = 0
$TotalSent = 0
$TotalRec = 0

$MailPerDay = @()
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010

Do {
$From = $From.AddDays(1)
$To = $From.AddDays(1)

$intSent = $intRec = $intSentSize = $intRecSize = 0

(Get-TransportServer) | Get-MessageTrackingLog -ResultSize Unlimited-Start $From-End $To | ForEach { 
# Sent E-mails 
If ($_.EventId -eq "RECEIVE" -and $_.Source-eq "STOREDRIVER") 
{ 
$intSent++ 
$intSentSize += $_.TotalBytes 
} 

# Received E-mails 
If ($_.EventId -eq "DELIVER") 
{ 
$intRec++ 
$intRecSize += $_.TotalBytes 
} 
}

$props = [ordered]@{ Date=$From
Sent=$intSent
SentSizeMB=[Math]::Round($intSentSize/1MB, 0)
Recived=$intRec
RecivedSizeMB=[Math]::Round($intRecSize/1MB, 0)
TotalPerDayInMB=[Math]::Round(($intRecSize+$intSentSize)/1MB, 2)
TotalPerDayInGB=[Math]::Round(($intRecSize+$intSentSize)/1GB, 2)
}

$obj = New-Object -TypeName PSObject -Property $props

$MailPerDay += $obj

$TotalSent += $intSentSize
$TotalRec += $intRecSize
} While ($To-lt (Get-Date)) 

$MailPerDay | ft

Write-Host "total sent during the reporting period $([Math]::Round( $TotalSent/1GB, 2)) GB"
Write-Host "total received during the reporting period $([Math]::Round( $TotalRec/1GB, 2)) Gigabit"
Write-Host "total received and sent for the period $([Math]::Round( ($TotalSent + $TotalRec)/1GB, 2)) GB"

To get a beautiful html report, for example to send by mail below augmented the script:



the

#### Variables #####
# the period in which we see the statistics, it is considered from today
$PeriodIndays = 7
# date at which you look, should be less than the starting
$EndPeriod = Get-date-hour 0-minute 0 -second 0
# if you write manually remember what the date is naobrot MM/DD/YYYY, ie "09/5/2016"
$StartPeriod = ($EndPeriod).AddDays( -$PeriodIndays )
# path to html file
$PathFile = 'c:\EmailSendAndreceived.html'

####################################################################################
$From = $StartPeriod
$To = $EndPeriod

[Int64] $intSent = 0
[Int64] $intRec = 0
[Int64] $intSentSize = 0
[Int64] $intRecSize = 0
$Total = 0
$TotalSent = 0
$TotalRec = 0

$MailPerDay = @()
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010

Do {
$From = $From.AddDays(1)
$To = $From.AddDays(1)

$intSent = $intRec = $intSentSize = $intRecSize = 0

(Get-TransportServer) | Get-MessageTrackingLog -ResultSize Unlimited-Start $From-End $To | ForEach { 
# Sent E-mails 
If ($_.EventId -eq "RECEIVE" -and $_.Source-eq "STOREDRIVER") 
{ 
$intSent++ 
$intSentSize += $_.TotalBytes 
} 

# Received E-mails 
If ($_.EventId -eq "DELIVER") 
{ 
$intRec++ 
$intRecSize += $_.TotalBytes 
} 
}

$props = [ordered]@{ Date=$From
Sent=$intSent
SentSizeMB=[Math]::Round($intSentSize/1MB, 0)
Recived=$intRec
RecivedSizeMB=[Math]::Round($intRecSize/1MB, 0)
TotalPerDayInMB=[Math]::Round(($intRecSize+$intSentSize)/1MB, 2)
TotalPerDayInGB=[Math]::Round(($intRecSize+$intSentSize)/1GB, 2)
}

$obj = New-Object -TypeName PSObject -Property $props

$MailPerDay += $obj

$TotalSent += $intSentSize
$TotalRec += $intRecSize
} While ($To-lt (Get-Date)) 

$MailPerDay | ft

Write-Host "total sent during the reporting period $([Math]::Round( $TotalSent/1GB, 2)) GB"
Write-Host "total received during the reporting period $([Math]::Round( $TotalRec/1GB, 2)) Gigabit"
Write-Host "total received and sent for the period $([Math]::Round( ($TotalSent + $TotalRec)/1GB, 2)) GB"

############# The HTML generating #############
$frag1 = $MailPerDay | sort-Property Date-Descending | ConvertTo-Html -As table-Fragment-PreContent "<h2>only received in the period $([Math]::Round( $TotalSent/1GB, 2)) <br> all sent for the period $([Math]::Round( $TotalRec/1GB, 2))<br>total sent and received during the period $([Math]::Round( ($TotalSent + $TotalRec)/1GB, 2))</h2>" | Out-String

Write-Verbose 'definiting CSS'
$head = @'
<style>
body { background-color:#ffffff; font-family:Tahoma; font-size:12pt; }
td, th { border:1px solid black; border-collapse:collapse; }
th { color:white; background-color:black; }
table, tr, td, th { padding: 2px; margin: 0px }
table { font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif; font-size: 14px; border-radius: 10px; border-spacing: 0; text-align: center; }
th { background: #BCEBDD; color: white; text-shadow: 0 1px 1px #2D2020; padding: 10px 20px; }
th, td { border-style: solid; border-width: 0 1px 1px 0; border-color: white; }
th:first-child, td:first-child { text-align: left; }
th:first-child { border-top-left-radius: 10px; }
th:last-child { border-top-right-radius: 10px; border-right: none; }
td { padding: 10px 20px; background: #F8E391; }
tr:last-child td:first-child { border-radius: 0 0 0 10px; }
tr:last-child td:last-child { border-radius: 0 0 10px 0; }
tr td:last-child { border-right: none; }
</style>
'@

$Date = Get-Date
$rep = ConvertTo-HTML -head $head-PostContent $frag1 -PreContent "<h1>Email Sent received $Date</h1>" | Out-String
$rep | Out-File $PathFile

The latter will generate a html file and put it in the default directory.
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

Fresh hay from the cow, or 3000 icons submitted!

Knowledge base. Part 2. Freebase: make requests to the Google Knowledge Graph

Group edit the resources (documents) using MIGXDB