Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.79 KB | None | 0 0
  1. #requires -version 2.0
  2.  
  3. $today = get-date
  4. $rundate = $($today.adddays(-1)).toshortdatestring()
  5.  
  6. $outfile_date = ([datetime]$rundate).tostring("yyyy_MM_dd")
  7. $outfile = "email_stats_" + $outfile_date + ".csv"
  8.  
  9. $dl_stat_file = "DL_stats.csv"
  10.  
  11. $accepted_domains = Get-AcceptedDomain |% {$_.domainname.domain}
  12. [regex]$dom_rgx = "`(?i)(?:" + (($accepted_domains |% {"@" + [regex]::escape($_)}) -join "|") + ")$"
  13.  
  14. $mbx_servers = Get-ExchangeServer |? {$_.serverrole -match "Mailbox"}|% {$_.fqdn}
  15. [regex]$mbx_rgx = "`(?i)(?:" + (($mbx_servers |% {"@" + [regex]::escape($_)}) -join "|") + ")\>$"
  16.  
  17. $msgid_rgx = "^\<.+@.+\..+\>$"
  18.  
  19. $hts = get-exchangeserver |? {$_.serverrole -match "hubtransport"} |% {$_.name}
  20.  
  21. $exch_addrs = @{}
  22.  
  23. $msgrec = @{}
  24. $bytesrec = @{}
  25.  
  26. $msgrec_exch = @{}
  27. $bytesrec_exch = @{}
  28.  
  29. $msgrec_smtpext = @{}
  30. $bytesrec_smtpext = @{}
  31.  
  32. $total_msgsent = @{}
  33. $total_bytessent = @{}
  34. $unique_msgsent = @{}
  35. $unique_bytessent = @{}
  36.  
  37. $total_msgsent_exch = @{}
  38. $total_bytessent_exch = @{}
  39. $unique_msgsent_exch = @{}
  40. $unique_bytessent_exch = @{}
  41.  
  42. $total_msgsent_smtpext = @{}
  43. $total_bytessent_smtpext = @{}
  44. $unique_msgsent_smtpext=@{}
  45. $unique_bytessent_smtpext = @{}
  46.  
  47. $dl = @{}
  48.  
  49.  
  50. $obj_table = {
  51. @"
  52. Date = $rundate
  53. User = $($address.split("@")[0])
  54. Domain = $($address.split("@")[1])
  55. Sent Total = $(0 + $total_msgsent[$address])
  56. Sent MB Total = $("{0:F2}" -f $($total_bytessent[$address]/1mb))
  57. Received Total = $(0 + $msgrec[$address])
  58. Received MB Total = $("{0:F2}" -f $($bytesrec[$address]/1mb))
  59. Sent Internal = $(0 + $total_msgsent_exch[$address])
  60. Sent Internal MB = $("{0:F2}" -f $($total_bytessent_exch[$address]/1mb))
  61. Sent External = $(0 + $total_msgsent_smtpext[$address])
  62. Sent External MB = $("{0:F2}" -f $($total_bytessent_smtpext[$address]/1mb))
  63. Received Internal = $(0 + $msgrec_exch[$address])
  64. Received Internal MB = $("{0:F2}" -f $($bytesrec_exch[$address]/1mb))
  65. Received External = $(0 + $msgrec_smtpext[$address])
  66. Received External MB = $("{0:F2}" -f $($bytesrec_smtpext[$address]/1mb))
  67. Sent Unique Total = $(0 + $unique_msgsent[$address])
  68. Sent Unique MB Total = $("{0:F2}" -f $($unique_bytessent[$address]/1mb))
  69. Sent Internal Unique = $(0 + $unique_msgsent_exch[$address])
  70. Sent Internal Unique MB = $("{0:F2}" -f $($unique_bytessent_exch[$address]/1mb))
  71. Sent External Unique = $(0 + $unique_msgsent_smtpext[$address])
  72. Sent External Unique MB = $("{0:F2}" -f $($unique_bytessent_smtpext[$address]/1mb))
  73. "@
  74. }
  75.  
  76. $props = $obj_table.ToString().Split("`n")|% {if ($_ -match "(.+)="){$matches[1].trim()}}
  77.  
  78. $stat_recs = @()
  79.  
  80. function time_pipeline {
  81. param ($increment = 1000)
  82. begin{$i=0;$timer = [diagnostics.stopwatch]::startnew()}
  83. process {
  84. $i++
  85. if (!($i % $increment)){Write-host “`rProcessed $i in $($timer.elapsed.totalseconds) seconds” -nonewline}
  86. $_
  87. }
  88. end {
  89. write-host “`rProcessed $i log records in $($timer.elapsed.totalseconds) seconds”
  90. Write-Host " Average rate: $([int]($i/$timer.elapsed.totalseconds)) log recs/sec."
  91. }
  92. }
  93.  
  94. foreach ($ht in $hts){
  95.  
  96. Write-Host "`nStarted processing $ht"
  97.  
  98. get-messagetrackinglog -Server $ht -Start "$rundate" -End "$rundate 11:59:59 PM" -resultsize unlimited |
  99. time_pipeline |%{
  100.  
  101.  
  102. if ($_.eventid -eq "DELIVER" -and $_.source -eq "STOREDRIVER"){
  103.  
  104. if ($_.messageid -match $mbx_rgx -and $_.sender -match $dom_rgx) {
  105.  
  106. $total_msgsent[$_.sender] += $_.recipientcount
  107. $total_bytessent[$_.sender] += ($_.recipientcount * $_.totalbytes)
  108. $total_msgsent_exch[$_.sender] += $_.recipientcount
  109. $total_bytessent_exch[$_.sender] += ($_.totalbytes * $_.recipientcount)
  110.  
  111. foreach ($rcpt in $_.recipients){
  112. $exch_addrs[$rcpt] ++
  113. $msgrec[$rcpt] ++
  114. $bytesrec[$rcpt] += $_.totalbytes
  115. $msgrec_exch[$rcpt] ++
  116. $bytesrec_exch[$rcpt] += $_.totalbytes
  117. }
  118.  
  119. }
  120.  
  121. else {
  122. if ($_messageid -match $messageid_rgx){
  123. foreach ($rcpt in $_.recipients){
  124. $msgrec[$rcpt] ++
  125. $bytesrec[$rcpt] += $_.totalbytes
  126. $msgrec_smtpext[$rcpt] ++
  127. $bytesrec_smtpext[$rcpt] += $_.totalbytes
  128. }
  129. }
  130.  
  131. }
  132.  
  133. }
  134.  
  135.  
  136. if ($_.eventid -eq "RECEIVE" -and $_.source -eq "STOREDRIVER"){
  137. $exch_addrs[$_.sender] ++
  138. $unique_msgsent[$_.sender] ++
  139. $unique_bytessent[$_.sender] += $_.totalbytes
  140.  
  141. if ($_.recipients -match $dom_rgx){
  142. $unique_msgsent_exch[$_.sender] ++
  143. $unique_bytessent_exch[$_.sender] += $_.totalbytes
  144. }
  145.  
  146. if ($_.recipients -notmatch $dom_rgx){
  147. $ext_count = ($_.recipients -notmatch $dom_rgx).count
  148. $unique_msgsent_smtpext[$_.sender] ++
  149. $unique_bytessent_smtpext[$_.sender] += $_.totalbytes
  150. $total_msgsent[$_.sender] += $ext_count
  151. $total_bytessent[$_.sender] += ($ext_count * $_.totalbytes)
  152. $total_msgsent_smtpext[$_.sender] += $ext_count
  153. $total_bytessent_smtpext[$_.sender] += ($ext_count * $_.totalbytes)
  154. }
  155.  
  156.  
  157. }
  158.  
  159. if ($_.eventid -eq "expand"){
  160. $dl[$_.relatedrecipientaddress] ++
  161. }
  162. }
  163.  
  164. }
  165.  
  166. foreach ($address in $exch_addrs.keys){
  167.  
  168. $stat_rec = (new-object psobject -property (ConvertFrom-StringData (&$obj_table)))
  169. $stat_recs += $stat_rec | select $props
  170. }
  171.  
  172. $stat_recs | export-csv $outfile -notype
  173.  
  174. if (Test-Path $dl_stat_file){
  175. $DL_stats = Import-Csv $dl_stat_file
  176. $dl_list = $dl_stats |% {$_.address}
  177. }
  178.  
  179. else {
  180. $dl_list = @()
  181. $DL_stats = @()
  182. }
  183.  
  184.  
  185. $DL_stats |% {
  186. if ($dl[$_.address]){
  187. if ([datetime]$_.lastused -le [datetime]$rundate){
  188. $_.used = [int]$_.used + [int]$dl[$_.address]
  189. $_.lastused = $rundate
  190. }
  191. }
  192. }
  193.  
  194. $dl.keys |% {
  195. if ($dl_list -notcontains $_){
  196. $new_rec = "" | select Address,Used,Since,LastUsed
  197. $new_rec.address = $_
  198. $new_rec.used = $dl[$_]
  199. $new_rec.Since = $rundate
  200. $new_rec.lastused = $rundate
  201. $dl_stats += @($new_rec)
  202. }
  203. }
  204.  
  205. $dl_stats | Export-Csv $dl_stat_file -NoTypeInformation -force
  206.  
  207.  
  208. Write-Host "`nRun time was $(((get-date) - $today).totalseconds) seconds."
  209. Write-Host "Email stats file is $outfile"
  210. Write-Host "DL usage stats file is $dl_stat_file"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement