Powershell set-transportrule

Arnone, Anthony 61 Reputation points
2021-06-29T00:14:18.657+00:00

I am running the following cmdlet to add specific rule to a users mailbox:
Set-TransportRule "Some rule" -From bad.user@g.com

This works fine but I want to be able to tell if for some reason the cmdlet failed, in this case for a bad email address but in the case above I am not getting back any kind of status whether failed or succeeded.

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,396 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 45,111 Reputation points
    2021-06-29T14:54:48.423+00:00

    Can you be more explicit about what you mean when you describe "bad.user@g.com" as a "bad e-mail address"?

    There is nothing "wrong" with that SMTP address except that it may not belong to a valid domain, or that the user portion of the address doesn't exist in the mail system that is responsible for the domain.

    If "g.com" is a domain managed by your Exchange organization you can verify that the address is assigned to some sort of mail-enabled object by using this:

    Get-Recipient bad.user@g.com
    

    Checking an e-mail address to see if it's properly constructed is not difficult, but it does involve checking the user portion for extraneous characters and the presence of consecutive "." characters. The domain portion can be checked by looking for the MX record(s) for the domain.

    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Rich Matheisen 45,111 Reputation points
    2021-06-29T01:29:49.48+00:00

    According to this page exchange-management-shell-cmdlet-input-and-output-types that cmdlet doesn't return anything.

    If you want to know if the cmdlet failed you can add "-ErrorAction Stop" to the cmdlet and surround it with Try/Catch blocks. Put whatever error handling you want into the "Catch" block.

    0 comments No comments

  2. Arnone, Anthony 61 Reputation points
    2021-06-29T02:12:26.81+00:00

    Thanks for the reply, I added the code you suggested but since like you said the cmdlet doesnt return anything no error is being thrown for the bad email address, I guess there is no way of knowing if it actually works or not.

    0 comments No comments

  3. Ian Xue (Shanghai Wicresoft Co., Ltd.) 31,016 Reputation points Microsoft Vendor
    2021-06-29T04:05:37.16+00:00

    Hi,

    If you want to know if the Set-TransportRule cmdlet actually ran successfully you can check the "from" property of the rule.

    (Get-TransportRule -Identity "testrule").from  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments