-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDetectInboxRulesThatForwardExternally.ps1
More file actions
55 lines (45 loc) · 2.13 KB
/
DetectInboxRulesThatForwardExternally.ps1
File metadata and controls
55 lines (45 loc) · 2.13 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
Function Connect-EXOnline {
$credentials = Get-Credential
Write-Output "Getting the Exchange Online cmdlets"
$Session = New-PSSession -ConnectionUri https://outlook.office365.com/powershell-liveid/ `
-ConfigurationName Microsoft.Exchange -Credential $credentials `
-Authentication Basic -AllowRedirection
Import-PSSession $Session
}
Connect-EXOnline
$domains = Get-AcceptedDomain
$mailboxes = Get-Mailbox -ResultSize Unlimited
foreach ($mailbox in $mailboxes) {
$forwardingRules = $null
Write-Host "Checking rules for $($mailbox.displayname) - $($mailbox.primarysmtpaddress)" -foregroundColor Green
$rules = get-inboxrule -Mailbox $mailbox.primarysmtpaddress
$forwardingRules = $rules | Where-Object {$_.forwardto -or $_.forwardasattachmentto}
foreach ($rule in $forwardingRules) {
$recipients = @()
$recipients = $rule.ForwardTo | Where-Object {$_ -match "SMTP"}
$recipients += $rule.ForwardAsAttachmentTo | Where-Object {$_ -match "SMTP"}
$externalRecipients = @()
foreach ($recipient in $recipients) {
$email = ($recipient -split "SMTP:")[1].Trim("]")
$domain = ($email -split "@")[1]
if ($domains.DomainName -notcontains $domain) {
$externalRecipients += $email
}
}
if ($externalRecipients) {
$extRecString = $externalRecipients -join ", "
Write-Host "$($rule.Name) forwards to $extRecString" -ForegroundColor Yellow
$ruleHash = $null
$ruleHash = [ordered]@{
PrimarySmtpAddress = $mailbox.PrimarySmtpAddress
DisplayName = $mailbox.DisplayName
RuleId = $rule.Identity
RuleName = $rule.Name
RuleDescription = $rule.Description
ExternalRecipients = $extRecString
}
$ruleObject = New-Object PSObject -Property $ruleHash
$ruleObject | Export-Csv C:\temp\externalrules.csv -NoTypeInformation -Append
}
}
}