2022-04-05 18:15:34 +05:30
<#
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
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
2023-10-06 17:23:22 +05:30
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 .
2022-04-05 18:15:34 +05:30
For detailed script execution : https : / / o365reports . com / 2020 / 08 / 12 / export-office - 365 -mail -traffic -report -with -powershell /
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
#>
Param
2020-10-21 18:42:06 +05:30
(
[ Parameter ( Mandatory = $false ) ]
2022-04-05 18:15:34 +05:30
[ switch ] $NoMFA ,
2020-10-21 18:42:06 +05:30
[ switch ] $OnlyOrganizationUsers ,
[ Nullable[DateTime] ] $StartDate ,
[ Nullable[DateTime] ] $EndDate ,
2022-04-05 18:15:34 +05:30
[ switch ] $MailsSent ,
[ switch ] $SpamsReceived ,
[ switch ] $MalwaresReceived ,
2020-10-21 18:42:06 +05:30
[ string ] $UserName ,
[ string ] $Password
)
2022-04-05 18:15:34 +05:30
Function Install_Modules
2020-10-21 18:42:06 +05:30
{
#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
2022-04-05 18:15:34 +05:30
Import-Module ExchangeOnlineManagement
2020-10-21 18:42:06 +05:30
}
else
{
Write-Host EXO V2 module is required to connect Exchange Online . Please install module using Install-Module ExchangeOnlineManagement cmdlet .
Exit
}
2022-04-05 18:15:34 +05:30
}
2020-10-21 18:42:06 +05:30
2022-04-05 18:15:34 +05:30
if ( $OnlyOrganizationUsers . IsPresent )
2020-10-21 18:42:06 +05:30
{
2022-04-05 18:15:34 +05:30
#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
}
}
2020-10-21 18:42:06 +05:30
}
2022-04-05 18:15:34 +05:30
}
2020-10-21 18:42:06 +05:30
2022-04-05 18:15:34 +05:30
Install_Modules
2020-10-21 18:42:06 +05:30
#Authentication using non-MFA
2022-04-05 18:15:34 +05:30
if ( $NoMFA . IsPresent )
2020-10-21 18:42:06 +05:30
{
#Storing credential in script for scheduling purpose/ Passing credential as parameter
if ( ( $UserName -ne " " ) -and ( $Password -ne " " ) )
2022-04-05 18:15:34 +05:30
{
2020-10-21 18:42:06 +05:30
$SecuredPassword = ConvertTo-SecureString -AsPlainText $Password -Force
$Credential = New-Object System . Management . Automation . PSCredential $UserName , $SecuredPassword
}
else
{
$Credential = Get-Credential -Credential $null
}
if ( $OnlyOrganizationUsers . IsPresent )
{
2022-04-05 18:15:34 +05:30
Write-Host " Connecting Azure AD... "
Connect-MsolService -Credential $Credential | Out-Null
2020-10-21 18:42:06 +05:30
}
2022-04-05 18:15:34 +05:30
Write-Host " Connecting Exchange Online PowerShell... "
Connect-ExchangeOnline -Credential $Credential
2020-10-21 18:42:06 +05:30
}
2022-04-05 18:15:34 +05:30
#Connect to Exchange Online and AzureAD module using MFA
elseif ( $OnlyOrganizationUsers . IsPresent )
2020-10-21 18:42:06 +05:30
{
2022-04-05 18:15:34 +05:30
Write-Host " Connecting Exchange Online PowerShell... "
Connect-ExchangeOnline
Write-Host " Connecting Azure AD... "
Connect-MsolService | Out-Null
2020-10-21 18:42:06 +05:30
}
2022-04-05 18:15:34 +05:30
#Connect to Exchange Online PowerShell
2020-10-21 18:42:06 +05:30
else
{
2022-04-05 18:15:34 +05:30
Write-Host " Connecting Exchange Online PowerShell... "
Connect-ExchangeOnline
2020-10-21 18:42:06 +05:30
}
2020-10-21 18:45:38 +05:30
2022-04-05 18:15:34 +05:30
[ DateTime ] $MaxStartDate = ( ( Get-Date ) . AddDays ( -89 ) ) . Date
<# Getting mail traffic data for past 90 days
if ( ( $StartDate -eq $null ) -and ( $EndDate -eq $null ) )
2020-10-21 18:45:38 +05:30
{
2022-04-05 18:15:34 +05:30
[ DateTime ] $EndDate = ( Get-Date ) . Date
[ DateTime ] $StartDate = $MaxStartDate
2020-10-21 18:45:38 +05:30
}
2022-04-05 18:15:34 +05:30
#>
2020-10-21 18:45:38 +05:30
2022-04-05 18:15:34 +05:30
#Getting start date to audit mail traffic report
While ( $true )
{
if ( $StartDate -eq $null )
2020-10-21 18:45:38 +05:30
{
2022-04-05 18:15:34 +05:30
$StartDate = Read-Host Enter start time for report generation '(Eg:03/28/2022)'
2020-10-21 18:45:38 +05:30
}
2022-04-05 18:15:34 +05:30
Try
2020-10-21 18:45:38 +05:30
{
2022-04-05 18:15:34 +05:30
$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
2020-10-21 18:45:38 +05:30
}
}
2022-04-05 18:15:34 +05:30
#Getting end date to audit emails report
While ( $true )
{
if ( $EndDate -eq $null )
2020-10-21 18:45:38 +05:30
{
2022-04-05 18:15:34 +05:30
$EndDate = Read-Host Enter End time for report generation '(Eg: 03/28/2022)'
2020-10-21 18:45:38 +05:30
}
2022-04-05 18:15:34 +05:30
Try
2020-10-21 18:45:38 +05:30
{
2022-04-05 18:15:34 +05:30
$Date = [ DateTime ] $EndDate
2020-10-21 18:45:38 +05:30
if ( $EndDate -lt ( $StartDate ) )
{
2022-04-05 18:15:34 +05:30
Write-Host End time should be later than start time -ForegroundColor Red
return
2020-10-21 18:45:38 +05:30
}
2022-04-05 18:15:34 +05:30
break
2020-10-21 18:45:38 +05:30
}
2022-04-05 18:15:34 +05:30
Catch
2020-10-21 18:45:38 +05:30
{
2022-04-05 18:15:34 +05:30
Write-Host ` nNot a valid date -ForegroundColor Red
2020-10-21 18:45:38 +05:30
}
2022-04-05 18:15:34 +05:30
}
2020-10-21 18:45:38 +05:30
2022-04-05 18:15:34 +05:30
$IntervalTimeInMinutes = 1440 #$IntervalTimeInMinutes=Read-Host Enter interval time period '(in minutes)'
[ DateTime ] $CurrentStart = $StartDate
[ DateTime ] $CurrentEnd = $CurrentStart . AddMinutes ( 1439 )
2020-10-21 18:45:38 +05:30
2022-04-05 18:15:34 +05:30
#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 )
2020-10-21 18:45:38 +05:30
{
2022-04-05 18:15:34 +05:30
$Domains = ( Get-MsolDomain ) . Name
2020-10-21 18:45:38 +05:30
}
2022-04-05 18:15:34 +05:30
}
2020-10-21 18:45:38 +05:30
2022-04-05 18:15:34 +05:30
elseif ( $SpamsReceived . IsPresent )
{
$Category = " TopSpamRecipient "
$Header = " Spams Received "
}
elseif ( $MalwaresReceived . IsPresent )
{
$Category = " TopMalwareRecipient "
$Header = " Malwares Received "
}
else
{
$Category = " TopMailRecipient "
$Header = " Mails Received "
}
#Connect_Modules
2023-10-06 17:23:22 +05:30
Write-Host Getting mail traffic data . . . ` n
2022-04-05 18:15:34 +05:30
#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 )
{
2020-10-21 18:45:38 +05:30
Do
2022-04-05 18:15:34 +05:30
{
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
2020-10-21 18:45:38 +05:30
$AggregatedMailTrafficData + = $MailTrafficData
$Page + +
} While ( $MailTrafficData . count -eq 5000 )
2022-04-05 18:15:34 +05:30
$Date = Get-Date ( $CurrentStart ) -Format MM / dd / yyyy
$DataCount = $AggregatedMailTrafficData . count
for ( $i = 0 ; $i -lt $DataCount ; $i + + )
2020-10-21 18:45:38 +05:30
{
2022-04-05 18:15:34 +05:30
$UPN = $AggregatedMailTrafficData [ $i ] . C1
2020-10-21 18:45:38 +05:30
if ( $OnlyOrganizationUsers . IsPresent )
{
2022-04-05 18:15:34 +05:30
$Domain = $UPN . Split ( " @ " ) | Select-Object -Index 1
2020-10-21 18:45:38 +05:30
if ( ( $Domain -in $Domains ) -eq $false )
{
continue
}
}
2022-04-05 18:15:34 +05:30
$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
2020-10-21 18:45:38 +05:30
}
2022-04-05 18:15:34 +05:30
$AggregatedMailTrafficData = @ ( )
$Page = 1
if ( [ datetime ] $CurrentEnd -ge [ datetime ] $EndDate )
2020-10-21 18:45:38 +05:30
{
2022-04-05 18:15:34 +05:30
break
}
$CurrentStart = $CurrentStart . AddMinutes ( $IntervalTimeInMinutes )
$CurrentEnd = $CurrentEnd . AddMinutes ( $IntervalTimeInMinutes )
if ( $CurrentStart -gt ( Get-Date ) )
2020-10-21 18:45:38 +05:30
{
2022-04-05 18:15:34 +05:30
break
}
2020-10-21 18:45:38 +05:30
}
2022-04-05 18:15:34 +05:30
#Disconnect Exchange Online session
Disconnect-ExchangeOnline -Confirm: $false -InformationAction Ignore -ErrorAction SilentlyContinue
if ( ( Test-Path -Path $OutputCSV ) -eq " True " )
{
2023-10-06 17:23:22 +05:30
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
2022-04-05 18:15:34 +05:30
$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 "
}
}