/
MailTrafficReport.ps1
294 lines (267 loc) · 8.34 KB
/
MailTrafficReport.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<#
=============================================================================================
Name: Export Office 365 mail traffic statistics by user report
Description: This script exports mails sent, received, spam received and malware received statistics by users to CSV file
Version: 3.0
Website: o365reports.com
Script Highlights:
~~~~~~~~~~~~~~~~~
1.The script can generate 5+ email statistics reports like emails sent, emails received, spam received, and malware received count.
2.The script uses modern authentication to connect to Exchange Online.
3.The script can be executed with MFA enabled account too.
4.Exports report results to CSV.
5.Allows you to generate email statistics reports for a custom period.
6.Automatically installs the EXO V2 module (if not installed already) upon your confirmation.
7.Allows you to filter the mail traffic report for organization users alone.
8.The script is scheduler-friendly. i.e., Credentials can be passed as a parameter.
For detailed script execution: https://o365reports.com/2020/08/12/export-office-365-mail-traffic-report-with-powershell/
============================================================================================
#>
Param
(
[Parameter(Mandatory = $false)]
[switch]$NoMFA,
[switch]$OnlyOrganizationUsers,
[Nullable[DateTime]]$StartDate,
[Nullable[DateTime]]$EndDate,
[switch]$MailsSent,
[switch]$SpamsReceived,
[switch]$MalwaresReceived,
[string]$UserName,
[string]$Password
)
Function Install_Modules
{
#Check for EXO v2 module inatallation
$Module = Get-Module ExchangeOnlineManagement -ListAvailable
if($Module.count -eq 0)
{
Write-Host Exchange Online PowerShell V2 module is not available -ForegroundColor yellow
$Confirm= Read-Host Are you sure you want to install module? [Y] Yes [N] No
if($Confirm -match "[yY]")
{
Write-host "Installing Exchange Online PowerShell module"
Install-Module ExchangeOnlineManagement -Repository PSGallery -AllowClobber -Force
Import-Module ExchangeOnlineManagement
}
else
{
Write-Host EXO V2 module is required to connect Exchange Online.Please install module using Install-Module ExchangeOnlineManagement cmdlet.
Exit
}
}
if($OnlyOrganizationUsers.IsPresent)
{
#Check for Azure AD module
$Module = Get-Module MsOnline -ListAvailable
if($Module.count -eq 0)
{
Write-Host MSOnline module is not available -ForegroundColor yellow
$Confirm= Read-Host Are you sure you want to install the module? [Y] Yes [N] No
if($Confirm -match "[yY]")
{
Write-host "Installing MSOnline PowerShell module"
Install-Module MSOnline -Repository PSGallery -AllowClobber -Force
Import-Module MSOnline
}
else
{
Write-Host MSOnline module is required to generate the report.Please install module using Install-Module MSOnline cmdlet.
Exit
}
}
}
}
Install_Modules
#Authentication using non-MFA
if($NoMFA.IsPresent)
{
#Storing credential in script for scheduling purpose/ Passing credential as parameter
if(($UserName -ne "") -and ($Password -ne ""))
{
$SecuredPassword = ConvertTo-SecureString -AsPlainText $Password -Force
$Credential = New-Object System.Management.Automation.PSCredential $UserName,$SecuredPassword
}
else
{
$Credential=Get-Credential -Credential $null
}
if($OnlyOrganizationUsers.IsPresent)
{
Write-Host "Connecting Azure AD..."
Connect-MsolService -Credential $Credential | Out-Null
}
Write-Host "Connecting Exchange Online PowerShell..."
Connect-ExchangeOnline -Credential $Credential
}
#Connect to Exchange Online and AzureAD module using MFA
elseif($OnlyOrganizationUsers.IsPresent)
{
Write-Host "Connecting Exchange Online PowerShell..."
Connect-ExchangeOnline
Write-Host "Connecting Azure AD..."
Connect-MsolService | Out-Null
}
#Connect to Exchange Online PowerShell
else
{
Write-Host "Connecting Exchange Online PowerShell..."
Connect-ExchangeOnline
}
[DateTime]$MaxStartDate=((Get-Date).AddDays(-89)).Date
<#Getting mail traffic data for past 90 days
if(($StartDate -eq $null) -and ($EndDate -eq $null))
{
[DateTime]$EndDate=(Get-Date).Date
[DateTime]$StartDate=$MaxStartDate
}
#>
#Getting start date to audit mail traffic report
While($true)
{
if ($StartDate -eq $null)
{
$StartDate=Read-Host Enter start time for report generation '(Eg:03/28/2022)'
}
Try
{
$Date=[DateTime]$StartDate
if($Date -ge $MaxStartDate)
{
break
}
else
{
Write-Host `nMail traffic can be retrieved only for past 90 days. Please select a date after $MaxStartDate -ForegroundColor Red
return
}
}
Catch
{
Write-Host `nNot a valid date -ForegroundColor Red
}
}
#Getting end date to audit emails report
While($true)
{
if ($EndDate -eq $null)
{
$EndDate=Read-Host Enter End time for report generation '(Eg: 03/28/2022)'
}
Try
{
$Date=[DateTime]$EndDate
if($EndDate -lt ($StartDate))
{
Write-Host End time should be later than start time -ForegroundColor Red
return
}
break
}
Catch
{
Write-Host `nNot a valid date -ForegroundColor Red
}
}
$IntervalTimeInMinutes=1440 #$IntervalTimeInMinutes=Read-Host Enter interval time period '(in minutes)'
[DateTime]$CurrentStart=$StartDate
[DateTime]$CurrentEnd=$CurrentStart.AddMinutes(1439)
#Check whether CurrentEnd exceeds EndDate
if($CurrentEnd -gt $EndDate)
{
$CurrentEnd=$EndDate
}
if($CurrentStart -eq $CurrentEnd)
{
Write-Host Start and end time are same.Please enter different time range -ForegroundColor Red
Exit
}
if($MailsSent.isPresent)
{
$Category="TopMailSender"
$Header="Mails Sent"
#Filter to get sender mail traffic for organization's users alone
if($OnlyOrganizationUsers.IsPresent)
{
$Domains=(Get-MsolDomain).Name
}
}
elseif($SpamsReceived.IsPresent)
{
$Category="TopSpamRecipient"
$Header="Spams Received"
}
elseif($MalwaresReceived.IsPresent)
{
$Category="TopMalwareRecipient"
$Header="Malwares Received"
}
else
{
$Category="TopMailRecipient"
$Header="Mails Received"
}
#Connect_Modules
Write-Host Getting mail traffic data... `n
#Output file declaration
$OutputCSV=".\Mail_Traffic_Report_$((Get-Date -format yyyy-MMM-dd-ddd` hh-mm` tt).ToString()).csv"
$AllMailTrafficData=@()
$AllMailTraffic=""
$AggregatedMailTrafficData=@()
$Page=1
While($True)
{
Do
{
Write-Progress -Activity "`n Retrieving mail traffic data for $CurrentStart"
$MailTrafficData=Get-MailTrafficSummaryReport -StartDate $CurrentStart -EndDate $CurrentEnd -Category $Category -Page $Page -PageSize 5000 | Select-Object C1,C2
$AggregatedMailTrafficData+=$MailTrafficData
$Page++
}While($MailTrafficData.count -eq 5000)
$Date=Get-Date($CurrentStart) -Format MM/dd/yyyy
$DataCount=$AggregatedMailTrafficData.count
for($i=0;$i -lt $DataCount ;$i++)
{
$UPN=$AggregatedMailTrafficData[$i].C1
if($OnlyOrganizationUsers.IsPresent)
{
$Domain=$UPN.Split("@") | Select-Object -Index 1
if(($Domain -in $Domains) -eq $false)
{
continue
}
}
$Count=$AggregatedMailTrafficData[$i].C2
$AllMailTraffic=@{'Date'=$Date;'User Principal Name'=$UPN;$Header=$Count}
$AllMailTrafficData= New-Object PSObject -Property $AllMailTraffic
$AllMailTrafficData | select Date,'User Principal Name',$Header | Export-Csv $OutputCSV -NoTypeInformation -Append
}
$AggregatedMailTrafficData=@()
$Page=1
if([datetime]$CurrentEnd -ge [datetime]$EndDate)
{
break
}
$CurrentStart=$CurrentStart.AddMinutes($IntervalTimeInMinutes)
$CurrentEnd=$CurrentEnd.AddMinutes($IntervalTimeInMinutes)
if($CurrentStart -gt (Get-Date))
{
break
}
}
#Disconnect Exchange Online session
Disconnect-ExchangeOnline -Confirm:$false -InformationAction Ignore -ErrorAction SilentlyContinue
if((Test-Path -Path $OutputCSV) -eq "True")
{
Write-Host " The Output file available in the current working directory with name:" -NoNewline -ForegroundColor Yellow;
Write-Host $OutputCSV
Write-Host `n~~ Script prepared by AdminDroid Community ~~`n -ForegroundColor Green
Write-Host "~~ Check out " -NoNewline -ForegroundColor Green; Write-Host "admindroid.com" -ForegroundColor Yellow -NoNewline; Write-Host " to get access to 1800+ Microsoft 365 reports. ~~" -ForegroundColor Green `n`n
$Prompt = New-Object -ComObject wscript.shell
$UserInput = $Prompt.popup("Do you want to open output file?",`
0,"Open Output File",4)
If ($UserInput -eq 6)
{
Invoke-Item "$OutputCSV"
}
}