Guest User

Untitled

a guest
Sep 18th, 2018
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. ##################################
  2. #Email Report Script
  3. #Runs Once per day as scheduled Task
  4. #Requires exchange management console
  5. #Gives snapshot of high volume mail
  6. #################################
  7.  
  8.  
  9. #### Exchange connection Chunk ####
  10. Write-Verbose "Loading the Exchange snapin"
  11.  
  12. Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 -ErrorAction SilentlyContinue
  13.  
  14. . $env:ExchangeInstallPath\bin\RemoteExchange.ps1
  15.  
  16. Connect-ExchangeServer -auto -AllowClobber
  17. ##############################
  18.  
  19. $sent_mail_threshold = 150
  20. $domain_threshold = 20
  21. $whitelist_path = 'C:\Users\rshannon3\Desktop\Email_Reports\email_whitelist.txt'
  22. $whitelist_contents = @()
  23. $start_date=[DateTime]::Now.AddHours(-30).toString()
  24. $end_date=[DateTime]::Now.AddHours(-6).toString()
  25.  
  26.  
  27. foreach($line in get-content $whitelist_path) { $whitelist_contents += $line }
  28.  
  29. #retrive all mail in last 24 hours
  30. $global:recent_mail = Get-TransportServer | Get-MessageTrackingLog -Start $start_date -end $end_date -EventId "Send" -resultsize unlimited |
  31. select timestamp, sender, recipients, messagesubject
  32.  
  33. #sort by count above threshold and not in whitelist
  34. $global:recent_senders = $recent_mail | Group-Object sender | where {$_.name -like "*.edu*" -and -not ( $whitelist_contents -contains $_.name -or $_.name -like 'Microsoft*@CCCD*' ) } | sort count -Descending
  35.  
  36.  
  37. #Report table
  38. $table = @()
  39.  
  40. #Populate Table
  41. for($c = 0; $c -lt $recent_senders.Count; $c++)
  42. {
  43. $count_column = 0 #$recent_senders.get($c).count
  44. $name_column = $recent_senders.get($c).name
  45.  
  46. $subjects_hash=@{}
  47.  
  48. $top_subjects=$recent_senders.get($c).group | group messagesubject <#where {$_.count -gt $sent_mail_threshold}#> | sort count -Descending #| Select-Object -First 1
  49. #Add threshold counter to include num of recipients
  50. $adjusted_message_count = 0;
  51. $top_subjects | ForEach-Object {
  52. $adjusted_message_count += $_.group.recipients.Count
  53. $subjects_hash.add($_.Name, $_.Group.recipients.Count)
  54. }
  55. $count_column = $adjusted_message_count
  56.  
  57.  
  58. $subject_column = ($subjects_hash.GetEnumerator() | Sort-Object -Property value -Descending | where {$_.Value -gt $sent_mail_threshold} | % { "$($_.Key) = $($_.Value)" }) -join "`n"
  59.  
  60. if( $adjusted_message_count -lt $sent_mail_threshold -or $subject_column.Length -lt 1) #If no subject sent over threshold , do not add this sender to the report
  61. { continue }
  62.  
  63. $domains=@{} ## Summary of each recipient domain. Stored in a hash format domain : number of emails
  64.  
  65. $recent_senders.group | where {$_.sender -eq $recent_senders.get($c).name} | #Populate table
  66. foreach-object {
  67. $_.recipients |
  68. foreach-object {
  69. $ind = $_.IndexOf("@")
  70. $dom = $_.Substring($ind+1)
  71. if($domains.ContainsKey($dom)) {
  72. $domains.Set_Item($dom, $domains.Get_Item($dom) + 1 )
  73. }
  74. else {
  75. $domains.Add($dom, 1)
  76. }
  77. }
  78. }
  79.  
  80. $domain_column = ($domains.GetEnumerator() | Where-Object {$_.Value -gt $domain_threshold} | Sort-Object -Property value -Descending | % { "$($_.Key) = $($_.Value)" }) -join "`n" #Sort and convert hashtable to string
  81. $table += ,@($count_column, $name_column, $subject_column, $domain_column) #Store in table
  82.  
  83. }
  84. $table = $table | Sort-Object @{Expression={$_[0]}} -Descending
  85. $path = "C:\Users\rshannon3\Desktop\Email_Reports\email_report $(get-date -format MM-dd-yyyy).txt"
  86.  
  87. Out-File $path -InputObject "Date From: $start_date To: $end_date `n Current Subject Threshold: $sent_mail_threshold `n" #Date Header
  88. if( $table.Count -lt 1) {
  89. Out-File $path -InputObject "No entries found. `n" -Append
  90. }
  91. else {
  92. Format-Table -Wrap -AutoSize -InputObject $table @{label="Count"; expression={$_[0]}},@{label="Sender"; expression={$_[1]}},@{label="Top Subject"; expression={$_[2]}},@{label="Domain Summary"; expression={$_[3]}} |
  93. Out-File $path -Append
  94. }
  95.  
  96. #############################################
  97. $username = ""
  98. $password = ""
  99. $secstr = New-Object -TypeName System.Security.SecureString
  100. $password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
  101. $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr
  102.  
  103. $From = ""
  104. $To = ""
  105. $self = ""
  106. $Attachment = "C:\Users\rshannon3\Desktop\Email_Reports\email_report $(get-date -format MM-dd-yyyy).txt"
  107. $Subject = "Email Report $(get-date -format MM-dd-yyyy)"
  108. $Body = "Report is attached."
  109. $SMTPServer = "smtp.gmail.com"
  110. $SMTPPort = "587"
  111. Send-MailMessage -From $From -to $To,$self -Subject $Subject `
  112. -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl `
  113. -Credential $cred -Attachments $Attachment
  114.  
  115. ###########################################################>
Add Comment
Please, Sign In to add comment