📜 ⬆️ ⬇️

The number of sent and received letters by day

There was a need to browse on the exchange server how many letters are sent and received by users by day with statistics in megabytes, after studying the Internet, the Nuno Mota script was not satisfied that the script makes text output, which is not convenient for constant use and for generating html reports. The script has been reworked:



Receiving the number of sent and received letters by day from the exchange server.

#### Variables ##### #      ,    $PeriodIndays = 7 #    ,      $EndPeriod = Get-date -hour 0 -minute 0 -second 0 #         //  "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 "     $([Math]::Round( $TotalSent/1GB, 2)) " Write-Host "     $([Math]::Round( $TotalRec/1GB, 2)) " Write-Host "      $([Math]::Round( ($TotalSent + $TotalRec)/1GB, 2)) " 

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


')
 #### Variables ##### #      ,    $PeriodIndays = 7 #    ,      $EndPeriod = Get-date -hour 0 -minute 0 -second 0 #         //  "09/5/2016" $StartPeriod = ($EndPeriod).AddDays( -$PeriodIndays ) #   html  $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 "     $([Math]::Round( $TotalSent/1GB, 2)) " Write-Host "     $([Math]::Round( $TotalRec/1GB, 2)) " Write-Host "      $([Math]::Round( ($TotalSent + $TotalRec)/1GB, 2)) " ############# HTML generating ############# $frag1 = $MailPerDay | sort -Property Date -Descending | ConvertTo-Html -As table -Fragment -PreContent "<h2>    $([Math]::Round( $TotalSent/1GB, 2)) <br>     $([Math]::Round( $TotalRec/1GB, 2))<br>      $([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 an html-file and put it by default in the catalog C.

Source: https://habr.com/ru/post/310348/


All Articles