Created
May 24, 2018 19:46
-
-
Save warfighter8/10301fc3fa0621dfa458dc16bb89c3d1 to your computer and use it in GitHub Desktop.
Get Mailboxes with forwarding inbox rules
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$users = (get-mailbox -resultsize unlimited).UserPrincipalName | |
foreach ($user in $users) | |
{ | |
Get-InboxRule -Mailbox $user | Select-Object MailboxOwnerID,Name,Description,Enabled,RedirectTo, MoveToFolder,ForwardTo | Export-CSV C:\temp\testing.csv -NoTypeInformation -Append | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment