0

I have a custom powershell script that connects to Exchange Online and tries to add spam email addresses & domains to the custom Content Filter Policy. The script is:

# Blocks Spammers - Email Address & Domains if required
$Spammers = "E:\scripts\Repository\Exchange-Online\Blocked-Email-Address.csv"
$SpamDomains = "E:\scripts\Repository\Exchange-Online\Blocked-Domains.csv"
$SpamPolicy = 'Corp-Custom-BlockList-For-AntiSPAM'
# Block Senders
Import-Csv $Spammers | foreach {Set-HostedContentFilterPolicy -Identity $SpamPolicy –BlockedSenders @{add=$_.SpamEmailAddresses}}
# Block Domains
Import-Csv $SpamDomains | foreach {Set-HostedContentFilterPolicy -Identity $SpamPolicy –BlockedSenderDomains @{add=$_.SpamDomains}}
# End of Script

The " E:\scripts\Repository\Exchange-Online\Blocked-Email-Address.csv " file has: Text

SpamEmailAddresses
123@345.com

When I run this as a powershell script I get the errors:

# Block Domains
Import-Csv E:\scripts\Repository\Exchange-Online\Blocked-Domains.csv | foreach {Set-HostedContentFilterPolicy -Identity My-Policy â€BlockedSenderDomains'.
    + CategoryInfo          : InvalidArgument: (:) [Set-HostedContentFilterPolicy], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Set-HostedContentFilterPolicy
    + PSComputerName        : outlook.office365.com

However, when I run the commands in powershell session manually, it succeeds

Please help to resolve

  • 1
    I edited the script in Notepad++ and it seems to have an issue. Copied the content to a new powershell script in Powershell ISE and it works – Prashant Nagaraj Apr 26 '20 at 11:25

1 Answers1

0

I am glad to know this issue is resolved, you could mark the reply as answer. Some similar script: https://community.spiceworks.com/scripts/show/4424-o365-add-sender-or-domain-to-spam-allow-list http://exchangetips.us/2017/12/add-blocked-senders-to-exchange-online-using-powershell/

Jayce
  • 769
  • 4
  • 5