-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmailForwardingReport.ps1
More file actions
184 lines (167 loc) · 9.69 KB
/
EmailForwardingReport.ps1
File metadata and controls
184 lines (167 loc) · 9.69 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
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
<#
=============================================================================================
Name: Export Office 365 Email Forwarding Report using PowerShell
Description: This script exports Office 365 email forwarding report to CSV format
Version: 1.0
Website: o365reports.com
Script Highlights:
~~~~~~~~~~~~~~~~~
1.Generates 3 different email forwarding rules reports.
2.Automatically installs the Exchange Online module upon your confirmation when it is not available in your machine.
3.Shows mailboxes in which email forwarding configured through ‘Forwarding SMTP Address’ and ‘Forward To’.
4.Lists all inbox rules that forward email to others’ mailbox.
5.Identifies transport rule that redirects emails to mailboxes
6.Supports both MFA and Non-MFA accounts.
7.Exports the report in CSV format.
8.The script is scheduler-friendly. You can automate the report generation upon passing credentials as parameters.
For detailed script execution: https://o365reports.com/2021/06/09/export-office-365-email-forwarding-report-using-powershell/
============================================================================================
#>
param(
[string] $UserName = $null,
[string] $Password = $null,
[Switch] $InboxRules,
[Switch] $MailFlowRules
)
Function GetPrintableValue($RawData) {
if (($null -eq $RawData) -or ($RawData.Equals(""))) {
return "-";
}
else {
$StringVal = $RawData | Out-String
return $StringVal;
}
}
Function GetAllMailForwardingRules {
Write-host `n"Preparing the Email Forwarding Report..."
if($InboxRules.IsPresent) {
$global:ExportCSVFileName = "InboxRulesWithEmailForwarding_" + ((Get-Date -format "MMM-dd hh-mm-ss tt").ToString()) + ".csv"
Get-Mailbox -ResultSize Unlimited | ForEach-Object {
Write-Progress "Processing the Inbox Rule for the User: $($_.Id)" " "
Get-InboxRule -Mailbox $_.PrimarySmtpAddress | Where-Object { $_.ForwardAsAttachmentTo -ne $Empty -or $_.ForwardTo -ne $Empty -or $_.RedirectTo -ne $Empty} | ForEach-Object {
$CurrUserRule = $_
GetInboxRulesInfo
}
}
}
Elseif ($MailFlowRules.IsPresent) {
$global:ExportCSVFileName = "TransportRulesWithEmailForwarding_" + ((Get-Date -format "MMM-dd hh-mm-ss tt").ToString()) + ".csv"
Get-TransportRule -ResultSize Unlimited | Where-Object { $_.RedirectMessageTo -ne $Empty } | ForEach-Object {
Write-Progress -Activity "Processing the Transport Rule: $($_.Name)" " "
$CurrEmailFlowRule = $_
GetMailFlowRulesInfo
}
}
else{
$global:ExportCSVFileName = "EmailForwardingReport_" + ((Get-Date -format "MMM-dd hh-mm-ss tt").ToString()) + ".csv"
Get-Mailbox -ResultSize Unlimited | Where-Object { $_.ForwardingSMTPAddress -ne $Empty -or $_.ForwardingAddress -ne $Empty} | ForEach-Object {
Write-Progress -Activity "Processing Mailbox Forwarding Rules for the User: $($_.Id)" " "
$CurrEmailSetUp = $_
GetMailboxForwardingInfo
}
}
}
Function GetMailboxForwardingInfo {
$global:ReportSize = $global:ReportSize + 1
$MailboxOwner = $CurrEmailSetUp.PrimarySMTPAddress
$DeliverToMailbox = $CurrEmailSetUp.DeliverToMailboxandForward
if ($null -ne $CurrEmailSetUp.ForwardingSMTPAddress) {
$CurrEmailSetUp.ForwardingSMTPAddress = GetPrintableValue (($CurrEmailSetUp.ForwardingSMTPAddress).split(":") | Select -Index 1)
}
$ForwardingSMTPAddress = GetPrintableValue $CurrEmailSetUp.ForwardingSMTPAddress
if ($null -ne $CurrEmailSetUp.ForwardingAddress){
$CurrEmailSetUp.ForwardingAddress = GetPrintableValue ($CurrEmailSetUp.ForwardingAddress)
}
$ForwardTo = GetPrintableValue $CurrEmailSetUp.ForwardingAddress
#ExportResults
$ExportResult = @{'Mailbox Name' = $MailboxOwner; 'Forwarding SMTP Address' = $ForwardingSMTPAddress;'Forward To' =$ForwardTo; 'Deliver To Mailbox and Forward' = $DeliverToMailbox}
$ExportResults = New-Object PSObject -Property $ExportResult
$ExportResults | Select-object 'Mailbox Name', 'Forwarding SMTP Address','Forward To','Deliver To Mailbox and Forward' | Export-csv -path $global:ExportCSVFileName -NoType -Append -Force
}
Function GetInboxRulesInfo {
$global:ReportSize = $global:ReportSize + 1
$MailboxOwner = $CurrUserRule.MailboxOwnerId
$RuleName = $CurrUserRule.Name
$Enable = $CurrUserRule.Enabled
$StopProcessingRules = $CurrUserRule.StopProcessingRules
if ($null -ne $CurrUserRule.RedirectTo) {
$CurrUserRule.RedirectTo = GetPrintableValue (($CurrUserRule.RedirectTo).split("[") | Select-Object -Index 0).Replace('"', '').Trim()
}
$RedirectTo = GetPrintableValue $CurrUserRule.RedirectTo
if ($null -ne $CurrUserRule.ForwardAsAttachmentTo) {
$CurrUserRule.ForwardAsAttachmentTo = GetPrintableValue (($CurrUserRule.ForwardAsAttachmentTo).split("[") | Select-Object -Index 0).Replace('"', '').Trim()
}
$ForwardAsAttachment = GetPrintableValue $CurrUserRule.ForwardAsAttachmentTo
if ($null -ne $CurrUserRule.ForwardTo) {
$CurrUserRule.ForwardTo = GetPrintableValue (($CurrUserRule.ForwardTo).split("[") | Select-Object -Index 0).Replace('"', '').Trim()
}
$ForwardTo = GetPrintableValue $CurrUserRule.ForwardTo
#ExportResults
$ExportResult = @{'Mailbox Name' = $MailboxOwner; 'Inbox Rule' = $RuleName; 'Rule Status' = $Enable; 'Forward As Attachment To' = $ForwardAsAttachment; 'Forward To' = $ForwardTo; 'Stop Processing Rules' = $StopProcessingRules; 'Redirect To' = $RedirectTo }
$ExportResults = New-Object PSObject -Property $ExportResult
$ExportResults | Select-object 'Mailbox Name', 'Inbox Rule', 'Forward To', 'Redirect To', 'Forward As Attachment To','Stop Processing Rules', 'Rule Status' | Export-csv -path $global:ExportCSVFileName -NoType -Append -Force
}
Function GetMailFlowRulesInfo {
$global:ReportSize = $global:ReportSize + 1
$RuleName = $CurrEmailFlowRule.Name
$State = $CurrEmailFlowRule.State
$Mode = $CurrEmailFlowRule.Mode
$Priority = $CurrEmailFlowRule.Priority
$StopProcessingRules = $CurrEmailFlowRule.StopRuleProcessing
if ($null -ne $CurrEmailFlowRule.RedirectMessageTo) {
$CurrEmailFlowRule.RedirectMessageTo = GetPrintableValue ($CurrEmailFlowRule.RedirectMessageTo).Replace('{}', '').Trim()
}
$RedirectTo = $CurrEmailFlowRule.RedirectMessageTo
#ExportResults
$ExportResult = @{'Mail Flow Rule Name' = $RuleName; 'State' = $State; 'Mode' = $Mode; 'Priority' = $Priority; 'Redirect To' = $RedirectTo; 'Stop Processing Rule' = $StopProcessingRules}
$ExportResults = New-Object PSObject -Property $ExportResult
$ExportResults | Select-object 'Mail Flow Rule Name','Redirect To', 'Stop Processing Rule','State', 'Mode', 'Priority' | Export-csv -path $global:ExportCSVFileName -NoType -Append -Force
}
Function ConnectToExchange {
$Exchange = (get-module ExchangeOnlineManagement -ListAvailable).Name
if ($Exchange -eq $null) {
Write-host "Important: ExchangeOnline PowerShell module is unavailable. It is mandatory to have this module installed in the system to run the script successfully."
$confirm = Read-Host Are you sure you want to install module? [Y] Yes [N] No
if ($confirm -match "[yY]") {
Write-host "Installing ExchangeOnlineManagement"
Install-Module ExchangeOnlineManagement -Repository PSGallery -AllowClobber -Force
Write-host "ExchangeOnline PowerShell module is installed in the machine successfully."
}
elseif ($confirm -cnotmatch "[yY]" ) {
Write-host "Exiting. `nNote: ExchangeOnline PowerShell module must be available in your system to run the script."
Exit
}
}
#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
Connect-ExchangeOnline -Credential $Credential -ShowProgress $false | Out-Null
}
else {
Connect-ExchangeOnline | Out-Null
}
Write-Host "ExchangeOnline PowerShell module is connected successfully"
#End of Connecting Exchange Online
}
ConnectToExchange
$global:ReportSize = 0
GetAllMailForwardingRules
Write-Progress -Activity "--" -Completed
if ((Test-Path -Path $global:ExportCSVFileName) -eq "True") {
Write-Host `n" The output file available in:" -NoNewline -ForegroundColor Yellow
Write-Host .\$global:ExportCSVFileName `n
Write-Host "The exported report has $global:ReportSize email forwarding configurations"
$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 "$global:ExportCSVFileName"
}
}
else {
Write-Host "No data found with the specified criteria"
}
Disconnect-ExchangeOnline -Confirm:$false -InformationAction Ignore -ErrorAction SilentlyContinue
Write-Host `n"Disconnected active ExchangeOnline session"
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