Powershell - the use of wildcard or similar in Set-Mailboxjunkemailconfiguration

Asked

Viewed 96 times

0

I have been trying for some time to discover on my own, from searching on various websites, foruns etc a solution that I think is relatively simple but unsuccessful.

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

Let’s assume that I have received mails from different users, or from the same user, but that they continuously change their domain.

An hour would be, for example,

[email protected]

then comes as

[email protected]

another time comes as

[email protected]

and another time appears as

[email protected]

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

[email protected]

I would like to at least block what arrive as 'mcsv.net', which would already help enough.

That is, the Powershell instruction could be as

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

So far, I’ve 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 since it changes all the time, it doesn’t do much good.

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

Grateful

Mauro

1 answer

1


The code below is far from good programming, but may be useful for those who need 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 found something quite valuable, using the Exchange Administration Center itself: there are rules in the mail stream that allow, just fill fields, tell which filter to use.

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

  • 1 - I joined the Exchange website at the Office365 Administration Center
  • 2 - I went to the Administrator section
  • 3 - I chose Exchange
  • 4 - I chose Email Flow
  • 5 - I chose rules
  • 6 - I clicked the button with the plus sign (+) to create a new rule
  • 7 - I gave her a name
  • 8 - In the 'Apply This Rule If...' field, I have chosen 'Sender address includes...'
  • 9 - I filled part of the domain name to no longer receive mails from them. To each different one, I pressed the sign to add this window.
  • 10 - In the 'Do the following...' field I checked 'Delete message without notifying recipient or sender' and also one more action with 'Send incident report to ' (here I put my corporate mail).
  • 11 - Save.

I embrace you all, Mauro

Browser other questions tagged

You are not signed in. Login or sign up in order to post.