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