Last active
February 3, 2022 20:02
-
-
Save dpo007/155783e6530617ba6c1fec37f6f8eedb to your computer and use it in GitHub Desktop.
PowerShell :: Import Barracuda Sender Policy CSV into MS365 Anti-Spam Allow//Block Lists
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
param ( | |
[string]$CsvPath = (Join-Path $PSScriptRoot 'AllowList.csv'), | |
[string]$MS365User = 'coolguy@coolcompany.com', | |
[string]$BarracudaQuarantineEmail = 'quarantine@ess.ca.barracudanetworks.com', | |
[switch]$IncludeBlockLists | |
) | |
$getSessions = Get-PSSession | Select-Object -Property State, Name | |
$isConnected = [bool](@($getSessions) -like '@{State=Opened; Name=ExchangeOnlineInternalSession*').Count -gt 0 | |
If (!$isConnected) { | |
Connect-ExchangeOnline -UserPrincipalName $MS365User | |
} | |
$csvContent = Get-Content -Path $CsvPath | |
# Keep only the first two columns, and fix 'Policy' column (2) header. | |
$trimmedCsvContent = $csvContent -replace '^(.*?,.*?),.*$', '$1' -replace 'Policy \(block', 'Policy' | |
# Convert to an array of objects. | |
$list = ConvertFrom-Csv $trimmedCsvContent | |
# Create individual, MS365 compatible lists. | |
$emailAllowList = [string[]]($list | Where-Object {($_.Policy -eq 'exempt') -and ($_."Email Address" -like '*@*') -and ($_."Email Address" -notmatch '\*')})."Email Address" | |
# Append Barracuda email address. | |
if ($BarracudaQuarantineEmail -notin $emailAllowList) { | |
Write-Host ('Appending Barracuda quarantine email address to Allow list...') | |
$emailAllowList += $BarracudaQuarantineEmail | |
} | |
$emailAllowList = $emailAllowList | Sort-Object | |
$domainAllowList = [string[]]($list | Where-Object {($_.Policy -eq 'exempt') -and ($_."Email Address" -notlike '*@*') -and ($_."Email Address" -notmatch '\*')})."Email Address" | Sort-Object | |
Write-Host ('Allowed Email Addresses: {0}' -f $emailAllowList.Count) | |
Write-Host ('Allowed Email Domains: {0}' -f $domainAllowList.Count) | |
Write-Host 'Updating (replacing) existing Allow lists...' | |
Set-HostedContentFilterPolicy -Identity 'Default' -AllowedSenders $emailAllowList -AllowedSenderDomains $domainAllowList | |
if ($IncludeBlockLists) { | |
$emailBlockList = [string[]]($list | Where-Object {($_.Policy -eq 'block') -and ($_."Email Address" -like '*@*') -and ($_."Email Address" -notmatch '\*')})."Email Address" | Sort-Object | |
$domainBlockList = [string[]]($list | Where-Object {($_.Policy -eq 'block') -and ($_."Email Address" -notlike '*@*') -and ($_."Email Address" -notmatch '\*')})."Email Address" | Sort-Object | |
Write-Host ('Blocked Email Addresses: {0}' -f $emailBlockList.Count) | |
Write-Host ('Blocked Email Domains: {0}' -f $domainBlockList.Count) | |
Write-Host 'Updating (replacing) existing Block lists...' | |
Set-HostedContentFilterPolicy -Identity 'Default' -BlockedSenders $emailBlockList -BlockedSenderDomains $domainBlockList | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment