-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEmail-Report.ps1
More file actions
56 lines (44 loc) · 1.91 KB
/
Email-Report.ps1
File metadata and controls
56 lines (44 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<#
.SYNOPSIS
Searches Exchange message tracking logs
.EXAMPLE
.\Email-Report.ps1 -Recipient user@domain.com -StartDate 1/1/2012 -EndDate 1/31/2012
.DESCRIPTION
Will find all HUB/CAS servers, search message tracking logs for a given date range,
then output "email_report.csv" with the results to the desktop.
#>
Param(
# Search for items to this e-mail address
[Parameter(Mandatory=$true)][String] $Recipient,
# Start date
[Parameter(Mandatory=$true)][String] $StartDate,
# End date
[Parameter(Mandatory=$true)][String] $EndDate
)
# Default output path is desktop
$outputDir = [Environment]::GetFolderPath("Desktop")
$outputName = "email_report.csv"
# Append time to start/end dates to include entire day
$StartDate += " 12:00:00 AM"
$EndDate += " 11:59:59 PM"
try{
Write-Host "Loading Exchange Snap-In"
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 -ea SilentlyContinue
Write-Host "Searching Hub/CAS Servers"
$serverList = Get-ExchangeServer | Where {
($_.ServerRole -like "*Hub*")
}
$msgTrackLogs = New-Object System.Collections.Generic.List[object]
# Loop through servers and search message tracking logs
foreach ($server in $serverList) {
Get-MessageTrackingLog -Recipients:$Recipient -Server "$server" -Start "$StartDate" -End "$EndDate" -EventID "RECEIVE" -ResultSize UNLIMITED | % { $msgTrackLogs.Add($_) }
Get-MessageTrackingLog -Sender:$Recipient -Server "$server" -Start "$StartDate" -End "$EndDate" -EventID "RECEIVE" -ResultSize UNLIMITED | % { $msgTrackLogs.Add($_) }
}
# Only select timestamp, sender, and subject
$msgTrackLogs | Sort-Object -Property Timestamp `
| Select Timestamp,Sender,MessageSubject -Unique `
| Export-Csv "$outputDir\$outputName" -NoTypeInformation
} catch {}
if ((Read-Host "File output to: $outputDir\$outputName Open? (y/N)") -ieq "y") {
Invoke-Item $outputDir\$outputName
}