-
Notifications
You must be signed in to change notification settings - Fork 73
/
Get-DetailedMessageStats.ps1
166 lines (140 loc) · 6.15 KB
/
Get-DetailedMessageStats.ps1
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
################################################################################################################################################################
# This script connects to Office 365 and retrieves detailed SMTP mail traffic statistics by user
# Requires Office 365 Wave 15
#
# Office365Username - Mandatory - Administrator login ID for the tenant we are querying
# Office365Password - Mandatory - Administrator login password for the tenant we are querying
#
# This script outputs the results to a CSV file called DetailedMessageStats.csv
#
# To run the script
#
# .\Get-DetailedMessageStats.ps1 -Office365Username [email protected] -Office365Password Password123
#
#
################################################################################################################################################################
#Accept input parameters
Param(
[Parameter(Position=0, Mandatory=$false, ValueFromPipeline=$true)]
[string] $Office365Username,
[Parameter(Position=1, Mandatory=$false, ValueFromPipeline=$true)]
[string] $Office365Password
)
$OutputFile = "DetailedMessageStats.csv"
#Did they provide creds? If not, ask them for it.
if (([string]::IsNullOrEmpty($Office365Username) -eq $false) -and ([string]::IsNullOrEmpty($Office365AdminPassword) -eq $false))
{
$SecureOffice365AdminPassword = ConvertTo-SecureString -AsPlainText $Office365AdminPassword -Force
#Build credentials object
$Office365Credentials = New-Object System.Management.Automation.PSCredential $Office365Username, $SecureOffice365AdminPassword
}
else
{
#Build credentials object
$Office365Credentials = Get-Credential
}
#Create remote Powershell session
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $Office365credentials -Authentication Basic –AllowRedirection
#Import the session
Import-PSSession $Session -AllowClobber | Out-Null
Write-Host "Collecting Recipients..."
#Collect all recipients from Office 365
$Recipients = Get-Recipient * -ResultSize Unlimited | select PrimarySMTPAddress
$MailTraffic = @{}
foreach($Recipient in $Recipients)
{
$MailTraffic[$Recipient.PrimarySMTPAddress.ToLower()] = @{}
}
$Recipients = $null
#Collect Message Tracking Logs (These are broken into "pages" in Office 365 so we need to collect them all with a loop)
$Messages = $null
$Page = 1
do
{
Write-Host "Collecting Message Tracking - Page $Page..."
$CurrMessages = Get-MessageTrace -PageSize 5000 -Page $Page | Select Received,SenderAddress,RecipientAddress,Size
$Page++
$Messages += $CurrMessages
}
until ($CurrMessages -eq $null)
Remove-PSSession $session
Write-Host "Crunching Results..."
#Read each message tracking entry and add it to a hash table
foreach($Message in $Messages)
{
if ($Message.SenderAddress -ne $null)
{
if ($MailTraffic.ContainsKey($Message.SenderAddress))
{
$MessageDate = Get-Date -Date $Message.Received -Format yyyy-MM-dd
if ($MailTraffic[$Message.SenderAddress].ContainsKey($MessageDate))
{
$MailTraffic[$Message.SenderAddress][$MessageDate]['Outbound']++
$MailTraffic[$Message.SenderAddress][$MessageDate]['OutboundSize'] += $Message.Size
}
else
{
$MailTraffic[$Message.SenderAddress][$MessageDate] = @{}
$MailTraffic[$Message.SenderAddress][$MessageDate]['Outbound'] = 1
$MailTraffic[$Message.SenderAddress][$MessageDate]['Inbound'] = 0
$MailTraffic[$Message.SenderAddress][$MessageDate]['InboundSize'] = 0
$MailTraffic[$Message.SenderAddress][$MessageDate]['OutboundSize'] += $Message.Size
}
}
}
if ($Message.RecipientAddress -ne $null)
{
if ($MailTraffic.ContainsKey($Message.RecipientAddress))
{
$MessageDate = Get-Date -Date $Message.Received -Format yyyy-MM-dd
if ($MailTraffic[$Message.RecipientAddress].ContainsKey($MessageDate))
{
$MailTraffic[$Message.RecipientAddress][$MessageDate]['Inbound']++
$MailTraffic[$Message.RecipientAddress][$MessageDate]['InboundSize'] += $Message.Size
}
else
{
$MailTraffic[$Message.RecipientAddress][$MessageDate] = @{}
$MailTraffic[$Message.RecipientAddress][$MessageDate]['Inbound'] = 1
$MailTraffic[$Message.RecipientAddress][$MessageDate]['Outbound'] = 0
$MailTraffic[$Message.RecipientAddress][$MessageDate]['OutboundSize'] = 0
$MailTraffic[$Message.RecipientAddress][$MessageDate]['InboundSize'] += $Message.Size
}
}
}
}
Write-Host "Formatting Results..."
#Build a table to format the results
$table = New-Object system.Data.DataTable "DetailedMessageStats"
$col1 = New-Object system.Data.DataColumn Date,([datetime])
$table.columns.add($col1)
$col2 = New-Object system.Data.DataColumn Recipient,([string])
$table.columns.add($col2)
$col3 = New-Object system.Data.DataColumn Inbound,([int])
$table.columns.add($col3)
$col4 = New-Object system.Data.DataColumn Outbound,([int])
$table.columns.add($col4)
$col5 = New-Object system.Data.DataColumn InboundSize,([int])
$table.columns.add($col5)
$col6 = New-Object system.Data.DataColumn OutboundSize,([int])
$table.columns.add($col6)
#Transpose hashtable to datatable
ForEach ($Recipient in $MailTraffic.keys)
{
$RecipientName = $Recipient
foreach($Date in $MailTraffic[$RecipientName].keys)
{
$row = $table.NewRow()
$row.Date = $Date
$row.Recipient = $RecipientName
$row.Inbound = $MailTraffic[$RecipientName][$Date].Inbound
$row.Outbound = $MailTraffic[$RecipientName][$Date].Outbound
$row.InboundSize = $MailTraffic[$RecipientName][$Date].InboundSize
$row.OutboundSize = $MailTraffic[$RecipientName][$Date].OutboundSize
$table.Rows.Add($row)
}
}
#Export data to CSV and Screen
$table | sort Date,Recipient,Inbound,Outbound, InboundSize, OutboundSize | Out-GridView -Title "Messages Sent By User"
$table | sort Date,Recipient,Inbound,Outbound, InboundSize, OutboundSize | export-csv $OutputFile
Write-Host "Results saved to $OutputFile"