Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
breakoutModewide
languagepowershell
<#
YOU MUST CONNECT to Exchange Online as a Global Admin for this script to run properly. 
Connect-ExchangeOnline
#>

# Prompt user to choose between adding or removing domains

$addDomains = Read-Host "Do you want to add or remove domains? (A/R)"
$addDomains = $addDomains.Trim()

function Write-ProgressHelper {
    param (
        [int]$StepNumber,
        [string]$Message
    )

    Write-Progress -Activity 'Updating SafeList' -Status $Message -PercentComplete (($StepNumber / $steps) * 100)
}
$script:steps = ([System.Management.Automation.PsParser]::Tokenize((Get-Content "$PSScriptRoot\$($MyInvocation.MyCommand.Name)"), [ref]$null) | Where-Object { $_.Type -eq 'Command' -and $_.Content -eq 'Write-ProgressHelper' }).Count

$stepCounter = 0

# Get all active user type mailboxes
Write-ProgressHelper -Message 'Getting mailboxes' -StepNumber ($stepCounter++)
$Mailboxes = Get-Mailbox -RecipientTypeDetails UserMailbox -ResultSize Unlimited
$totalMailboxes = $Mailboxes.Count
Write-Output "Total mailboxes: $totalMailboxes"
$processedMailboxes = 0

# Configure junk email settings for each mailbox
if ($addDomains -eq "A" -or $addDomains -eq "a") {
  $JunkEmailConfig = @{
      Add = "ssuport.com", "supppot.com", "center-supports.com", "supoorts.com", "suporrt.com", "team-support.net", "supppot.net", "legal-user.com", "the-verification.com", "legals-team.com", "authenticatecenter.com", "ourlogin.co.uk", "mnminfo.com", "help-desc.com", "account-protector.com", "calendarsupdates.com", "appstatusupdate.com", "hr-updates.net", "itservicesector.com", "mailer-sender.com", "servicealerts.net", "status-pager.com", "nonreplies.com", "registerconfirmation.com"
  }
}
else {
    $JunkEmailConfig = @{
        RemoveAdd = "ssuport.com", "supppot.com", "center-supports.com", "supoorts.com", "suporrt.com", "team-support.net", "supppot.net", "legal-user.com", "the-verification.com", "legals-team.com", "authenticatecenter.com", "ourlogin.co.uk", "mnminfo.com", "help-desc.com", "account-protector.com", "calendarsupdates.com", "appstatusupdate.com", "hr-updates.net", "itservicesector.com", "mailer-sender.com", "servicealerts.net", "status-pager.com", "nonreplies.com", "registerconfirmation.com"
    }
}

# Configure junk email settings for each mailbox
Write-ProgressHelper -Message 'Configuring junk email settings' -StepNumber ($stepCounter++)
$i = 0
foreach ($Mailbox in $Mailboxes) {
    $MailboxIdentity = $Mailbox.PrimarySmtpAddress.ToString()
    Set-MailboxJunkEmailConfiguration -Identity $MailboxIdentity -TrustedSendersAndDomains $JunkEmailConfig
    $processedMailboxes++
    if ($addDomains -eq "A" -or $addDomains -eq "a") {

        Write-Progress -Id 1 -Activity "Working..." -status "Added: $i of $($Mailboxes.Count)" -percentComplete (($i / $Mailboxes.Count) * 100)
        $i++
    }
    else {
        Write-Progress -Id 1 -Activity "Working..." -status "Removed: $i of $($Mailboxes.Count)" -percentComplete (($i / $Mailboxes.Count) * 100)
        $i++
    }
}
Write-Output "Processed mailboxes: $processedMailboxes"

...