-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGet-ForwardingInboxRules.ps1
157 lines (142 loc) · 7.5 KB
/
Get-ForwardingInboxRules.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
<#
.SYNOPSIS
Get all inbox rules that forward (including as an attachment) or redirect.
.DESCRIPTION
Outputs all Inbox Rules (a.k.a., Outlook rules) where ForwardTo, FowardAsAttachmentTo, or RedirectTrue are defined.
Regardless of which action it is (or are, if multiple), an individual object is output for each target recipient.
The output objects are commonized, and a property - ForwardOrRedirect - will show which type it is. The
RoutingType property of the target recipient is the one which reveals whether it is an internal (RoutingType = 'EX')
or external (RoutingType 'SMTP').
The intent is to help with planning for external forwarding, as it relates to EOP Outbound spam protection policies,
remote domains, and transport rules. See the referenced Exchange Team blog post in the .LINK section
.PARAMETER Identity
Specifies the mailbox to check for forwarding/redirecting rules.
.PARAMETER All
Switch to indicate all mailboxes should be checked for forwarding/redirecting rules.
.EXAMPLE
Get-Mailbox -RecipientTypeDetails SharedMailbox | .\Get-ForwardingInboxRules.ps1 | fl
.EXAMPLE
.\Get-ForwardingInboxRules.ps1 -Identity [email protected]
.EXAMPLE
$Rules = .\Get-ForwardingInboxRules.ps1 -All; $Rules | Export-Csv $Home\Desktop\FwdInboxRules.csv -NTI
.LINK
https://techcommunity.microsoft.com/t5/exchange-team-blog/all-you-need-to-know-about-automatic-email-forwarding-in/ba-p/2074888
#>
[CmdletBinding(DefaultParameterSetName = 'Identity')]
param (
[Parameter(
ParameterSetName = 'Identity',
Position = 1,
Mandatory,
ValueFromPipeline,
ValueFromPipelineByPropertyName
)]
[object[]]$Identity,
[Parameter(ParameterSetName = 'All')]
[switch]$All
)
begin {
if ((Get-Command Get-Mailbox, Get-InboxRule -ErrorAction SilentlyContinue).Count -ne 2) {
throw 'An active Exchange PowerShell session is required, along with access to the Get-Mailbox and Get-InboxRule cmdlets.'
}
$Script:startTime = [datetime]::Now
$Script:stopwatchMain = [System.Diagnostics.Stopwatch]::StartNew()
$Script:stopwatchPipeline = [System.Diagnostics.Stopwatch]::new()
$Script:progress = @{
Id = 0
Activity = "$($PSCmdlet.MyInvocation.MyCommand.Name)"
Status = "Start time: $($startTime.ToString('yyyy-MM-ddTHH:mm:ss')) | Elapsed: $($stopWatchMain.Elapsed.ToString('hh\:mm\:ss'))"
PercentComplete = -1
}
Write-Progress @progress
if ($PSCmdlet.ParameterSetName -eq 'All') {
try { $Script:Mailboxes = Get-Mailbox -ResultSize Unlimited -ea:Stop }
catch { throw }
}
function getRules ([object[]]$mailbox) {
try {
foreach ($mbx in $mailbox) {
$_rules = $null;
$_rules = Get-InboxRule -Mailbox $mbx.PrimarySmtpAddress.ToString() -ea:SilentlyContinue |
Where-Object { $_.ForwardTo -or $_.ForwardAsAttachmentTo -or $_.RedirectTo }
if ($_rules) {
foreach ($_rule in $_rules) {
foreach ($_fwd in $_rule.ForwardTo) {
$__fwd = getFwdDetails -fwdObject $_fwd
[PSCustomObject]@{
mbxDisplayName = $mbx.DisplayName
mbxPrimarySmtpAddress = $mbx.PrimarySmtpAddress
ruleIdentity = $_rule.Identity
ruleEnabled = $_rule.Enabled
ruleDescription = $_rule.Description
ForwardOrRedirect = 'Forward'
ruleForwardToDisplayName = $__fwd.DisplayName
ruleForwardToAddr = $__fwd.Address
ruleForwardToRoutingType = $__fwd.RoutingType
}
}
foreach ($_fwd in $_rule.ForwardAsAttachmentTo) {
$__fwd = getFwdDetails -fwdObject $_fwd
[PSCustomObject]@{
mbxDisplayName = $mbx.DisplayName
mbxPrimarySmtpAddress = $mbx.PrimarySmtpAddress
ruleIdentity = $_rule.Identity
ruleEnabled = $_rule.Enabled
ruleDescription = $_rule.Description
ForwardOrRedirect = 'ForwardAsAttachment'
ruleForwardToDisplayName = $__fwd.DisplayName
ruleForwardToAddr = $__fwd.Address
ruleForwardToRoutingType = $__fwd.RoutingType
}
}
foreach ($_rdr in $_rule.RedirectTo) {
$__rdr = getFwdDetails -fwdObject $_rdr
[PSCustomObject]@{
mbxDisplayName = $mbx.DisplayName
mbxPrimarySmtpAddress = $mbx.PrimarySmtpAddress
ruleIdentity = $_rule.Identity
ruleEnabled = $_rule.Enabled
ruleDescription = $_rule.Description
ForwardOrRedirect = 'Redirect'
ruleForwardToDisplayName = $__rdr.DisplayName
ruleForwardToAddr = $__rdr.Address
ruleForwardToRoutingType = $__rdr.RoutingType
}
}
}
}
}
}
catch { throw }
}
function getFwdDetails ([object]$fwdObject) {
# This function massages data when we're using a standard Remote PowerShell session instead of the Exchange Management Shell.
if ($fwdObject.getType().Name -eq 'String') {
$fwdStringDetails = $fwdObject -replace '^"' -replace '\]' -split '" \[' -split ':'
[PSCustomObject]@{
DisplayName = $fwdStringDetails[0]
Address = $fwdStringDetails[2]
RoutingType = $fwdStringDetails[1]
}
}
else { $fwdObject }
}
$stopWatchPipeline.Start()
$Script:pipelineCounter = 0
}
process {
$pipelineCounter++
if ($PSCmdlet.ParameterSetName -eq 'Identity') { $Script:Mailboxes = if ($Identity[0].PrimarySmtpAddress) { $Identity[0] } else { try { Get-Mailbox $Identity[0] -ea:Stop } catch { throw } } }
$Mailboxes | ForEach-Object {
if ($stopWatchPipeline.ElapsedMilliseconds -ge 200) {
$Script:progress.Status = "Start time: $($startTime.ToString('yyyy-MM-ddTHH:mm:ss')) | Elapsed: $($stopWatchMain.Elapsed.ToString('hh\:mm\:ss'))"
$Script:progress.CurrentOperation = "Mailbox: $($_.DisplayName) ($($_.PrimarySmtpAddress))"
Write-Progress @progress
$_pct = if ($PSCmdlet.ParameterSetName -eq 'All') { (($pipelineCounter / $Mailboxes.Count) * 100) } else { -1 }
Write-Progress -Activity 'Getting forward/redirect rules...' -Id 1 -ParentId 0 -PercentComplete $_pct
$stopWatchPipeline.Restart()
}
try { getRules -mailbox $_ } catch { throw }
}
}
end { Write-Progress @progress -Completed }