Connect-ExoPSSession
$domains = Get-AcceptedDomain
$mailboxes = Get-DistributionGroupMember -Identity COM-NetIDRecipients -ResultSize Unlimited|Where {$_.RecipientType -like 'UserMailbox'}|%{get-mailbox -Identity $_.Name}
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
}
}
}