Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions ExchangeOnline/New-EXOExternalDisclaimerTransportRule.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ function Install-RequiredModules {
try {
Install-Module -Name $Module -Force -AllowClobber -Scope CurrentUser -ErrorAction Stop
Write-Information "Successfully installed $Module" -InformationAction Continue
}
catch {
} catch {
throw "Failed to install required module '$Module': $($_.Exception.Message). Please run 'Install-Module -Name $Module' manually or ensure you have appropriate permissions."
}
}
Expand All @@ -103,14 +102,12 @@ function Test-ExchangeOnlineConnection {
try {
Get-OrganizationConfig -ErrorAction Stop | Out-Null
Write-Verbose "Connected to Exchange Online"
}
catch {
} catch {
Write-Information "Not connected to Exchange Online. Attempting to connect..." -InformationAction Continue
try {
Connect-ExchangeOnline -ShowProgress:$false -ErrorAction Stop
Write-Information "Successfully connected to Exchange Online" -InformationAction Continue
}
catch {
} catch {
throw "Failed to connect to Exchange Online: $($_.Exception.Message). Please ensure you have the necessary permissions and network connectivity."
}
}
Expand All @@ -130,7 +127,7 @@ function Set-ExternalDisclaimerRule {
$existing = Get-TransportRule -Identity $RuleName -ErrorAction SilentlyContinue

$ruleParams = @{
Comments = "External email disclaimer per ArchiTech Labs methodology (https://www.architechlabs.io). Prevents duplicates via header stamp. Blog: https://www.architechlabs.io/articles/external-email-banner/"
Comments = "Appends disclaimer on all inbound external mail. Adds $HeaderName header to mark processing and prevent dupes. Exceptions only via auth results (DKIM > SPF > Return-Path). Focused on accessibility, consistency, security, and usability."
Priority = $Priority
FromScope = 'NotInOrganization'
SentToScope = 'InOrganization'
Expand All @@ -142,15 +139,15 @@ function Set-ExternalDisclaimerRule {
ExceptIfHeaderMatchesMessageHeader = $HeaderName
ExceptIfHeaderMatchesPatterns = $HeaderValue
Enabled = -not $Disabled
SenderAddressLocation = 'Envelope'
}

$action = if (-not $existing) { "Creating" } else { "Updating" }
Write-Information "$action transport rule: $RuleName" -InformationAction Continue

if (-not $existing) {
New-TransportRule -Name $RuleName @ruleParams -ErrorAction Stop
}
else {
} else {
Set-TransportRule -Identity $RuleName @ruleParams -ErrorAction Stop
}

Expand Down Expand Up @@ -182,25 +179,30 @@ $BannerHtml = @'
<table role="presentation" width="100%" border="0" cellspacing="0" cellpadding="0" style="mso-table-lspace:0;mso-table-rspace:0;">
<tr>
<td align="left" style="mso-table-lspace:0;mso-table-rspace:0;">
<!-- Width-capped container for comfortable line length -->
<table role="presentation" border="0" cellspacing="0" cellpadding="0" width="760" style="width:100%;max-width:760px;mso-table-lspace:0;mso-table-rspace:0;">
<tr>
<td style="mso-table-lspace:0;mso-table-rspace:0;">

<div dir="ltr" lang="en" role="note" aria-label="External email warning"
style="-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%;-moz-text-size-adjust:100%;
mso-line-height-rule:exactly;border:2px solid #d79c2b;
padding:8px;background:transparent;color:inherit;
font-family:Arial,Helvetica,sans-serif;font-size:15px;line-height:1.5;">
<strong>⚠️ External Email – Check Before You&nbsp;Act</strong><br><br>
This email is from <strong>outside our&nbsp;organization</strong>.<br>
• Do not reply, click links, or open attachments unless you trust the&nbsp;sender.<br>
• If it looks like it came from someone inside, confirm another way before acting.<br>
• Report suspicious messages using the <strong>REPORT</strong>&nbsp;button.
<strong>⚠️External Email – Verify Before You Act⚠️</strong><br><br>
This email is from <strong>outside the organization</strong>.<br>
• Do not reply, click links, or open attachments unless you trust the sender.<br>
• If it appears to be from someone inside the organization, confirm via another method before taking action.<br>
• Report suspicious messages using the <strong>Report</strong> button.
</div>

<!-- Spacer: consistent gap below the banner in all Outlook clients -->
<div style="line-height:0;font-size:0;" aria-hidden="true">
<table role="presentation" border="0" cellpadding="0" cellspacing="0" width="100%" style="mso-table-lspace:0;mso-table-rspace:0;">
<tr><td style="height:8px;line-height:8px;font-size:8px;">&nbsp;</td></tr>
</table>
</div>

</td>
</tr>
</table>
Expand All @@ -220,12 +222,10 @@ try {
if ($Disabled) {
Write-Warning "The rule '$RuleName' is created but DISABLED. Enable it when ready to activate."
Write-Information "To enable: Set-TransportRule -Identity '$RuleName' -Enabled `$true" -InformationAction Continue
}
else {
} else {
Write-Information "The rule '$RuleName' is now active with NO authentication exceptions." -InformationAction Continue
}
}
catch {
} catch {
Write-Error "Failed to configure transport rule: $($_.Exception.Message)"
Write-Warning "Common issues: Insufficient Exchange Online permissions, network connectivity, rule name conflicts, or transport rule size limits"
throw
Expand Down
Loading