Powershell - the use of wildcard or similar thing in Set-MailboxJunkEmailConfiguration

0

I've been trying to figure out on my own, from researching on various websites, forums etc, a solution I deem to be relatively simple, but without success.

I would like to block emails for a particular Exchange user, but not by the entire address, but by the domain name.

Let's assume that I have received emails from different users, or from the same user, but that it continually changes your domain.

One hour would be, for example,

... @ mail21.us4.mcsv.net

Then it arrives as

... @ mail68.atl71.mcdlv.net

Another time comes as

... @ mail21.wdc03.rsgsv.net

and another time appears as

... @ mail22.wdc01.mcdlv.net

and repeating part of one of the above domains, such as

... @ mail67.us4.mcsv.net

I would like to at least block what I get as 'mcsv.net', which would already help a lot.

That is, the PowerShell statement could be like

Set-MailboxJunkEmailConfiguration -Identity $usuario -BlockedSendersAndDomains @{Add="*.mcsv.net"}  

So far, I have seen that I can dial, for example,

 Set-MailboxJunkEmailConfiguration -Identity $usuario -BlockedSendersAndDomains @{Add="mail22.wdc01.mcdlv.net"}  

Because, in this case, this would be the full domain name.

But as it changes all the time, this does not help much.

Would you use wildcard for this problem? The instruction, the lock command itself, does not allow.

Thankful

Mauro

    
asked by anonymous 21.10.2015 / 12:42

1 answer

1

The code below is far from good programming, but can be useful for anyone who needs a light.

cls #limpa a tela para evitar confusão na resposta
$UserCredential = Get-Credential -Credential "[email protected]" #login/endereço do usuário com poderes totais
$usuario="[email protected]" #endereço da caixa postal a ser modificada
$dominioBloqueio="mcsv.net" #parte de nome de domínio que será bloqueado
$pasta=$usuario+":\Lixo Eletrônico" #para para direcionar as mensagens bloqueadas
$nomeDaRegra="REJEITAR" #nome da regra. Um nome qualquer. O que desejar
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential    $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session
$resultado=Get-Mailbox $usuario | Get-MailboxPermission -User "loginDoUsuarioAdministrador"
$direito="FullAccess" #No caso, tem de ser este direito completo
$booTemDireitosTotais=$false #flag que vai indicar se o direito já existe
ForEach($i in $resultado){#Procura todos os direitos existentes
if($i.AccessRights -eq $direito){
$booTemDireitosTotais=$true #existindo, marca o flag
}#if($i
}#forEach
$regras = Get-InboxRule -MailBox $usuario
$contador=0
foreach($i in $regras){
$contador=$contador+1
if($i.name -eq $nomeDaRegra){
$booRegraExiste=$true
}#$if($i.name
}
if ($booTemDireitosTotais){
#---
        if($contador -eq 0){
            Write-Host "O agente tem direitos totais na caixa-postal do usuário, mas não existe sequer uma regra regristrada. Basta criar a regra."
            New-InboxRule -name $nomeDaRegra -MailBox $usuario -HeaderContainsWords $dominioBloqueio -FromAddressContainsWords $dominioBloqueio -MoveToFolder $pasta -Confirm:$false
            Get-InboxRule  -Identity $nomeDaRegra -Mailbox $usuario
            Write-Host "Veja a propriedade Name acima para se certificar de que foi dada a permissão e criada a regra."
            } else{
            Write-host "Existem regras. Mas será que existe a que queremos?"
            #Há alguma regra, mas será que queremos?
                if($booRegraExiste){
                    Write-Host "A permissão está dada, a regra $nomeDaRegra existe, portanto basta altera-la"
                    Set-InboxRule -Identity $nomeDaRegra -Mailbox $usuario -HeaderContainsWords $dominioBloqueio -FromAddressContainsWords $dominioBloqueio -MoveToFolder $pasta -Confirm:$true
                    Get-InboxRule  -Identity $nomeDaRegra -Mailbox $usuario
                Write-Host "Veja a propriedade Name acima para se certificar de que foi alterada a regra."
                }else{
                    #a regra Não existe. Cria
                     Write-Host " A permissão está dada, mas a regra $nomeDaRegra NÃO EXISTE. Cria."
                    New-InboxRule -name $nomeDaRegra -MailBox $usuario -HeaderContainsWords $dominioBloqueio -FromAddressContainsWords $dominioBloqueio -MoveToFolder $pasta -Confirm:$false
                    Get-InboxRule  -Identity $nomeDaRegra -Mailbox $usuario
                    Write-Host "Veja a propriedade Name acima para se certificar de que foi alterada a regra."
                }#if ($booRegraExiste
        }#if($contador
#---
}else{
    Write-Host "O agente NÃO TEM direitos totais na caixa-postal do usuário. Vai ter de alterar esta configuração. Em seguida, criar a regra"
        Add-MailboxPermission -Identity $usuario -User "LoginUsuarioComPoderes" -AccessRights FullAccess -InheritanceType All
        New-InboxRule -name $nomeDaRegra -MailBox $usuario -HeaderContainsWords $dominioBloqueio -FromAddressContainsWords $dominioBloqueio -MoveToFolder $pasta -Confirm:$false
        Get-InboxRule  -Identity $nomeDaRegra -Mailbox $usuario
Write-Host "Veja a propriedade Name acima para se certificar de que foi dada a permissão e criada a regra."
}#if($booTem

Remove-PSSession $Session

For those who do not want to use PowerShell, I've found something quite valuable, using the Exchange Administration Center itself: there are rules in the mail flow that allow you to fill in fields, tell which filter to use.

In the latter case, I did the following to stay in ALL the mailboxes of my domain:

  • 1 - I joined the Exchange site in the Office365 Administration Center
  • 2 - I went to the Admin section
  • 3 - I chose Exchange
  • 4 - I chose E-mail Flow
  • 5 - I chose Rules
  • 6 - I clicked the plus sign (+) to create a new rule
  • 7 - I gave her a name
  • 8 - In the 'Apply This Rule If ...' field, I chose 'The sender address includes ...'
  • 9 - I have filled in part of the domain name to no longer receive mail from them. To each one different, I was pressing the plus sign of this window.
  • 10 - In the 'Do this ...' field, I marked 'Delete the message without notifying the addressee or sender' and also another action with 'Send incident report to' (here I put my corporate mail ).
  • 11 - Save.

Hugs to all, Mauro

    
22.10.2015 / 13:04