question

ArnoneAnthony-5113 avatar image
0 Votes"
ArnoneAnthony-5113 asked RichMatheisen-8856 answered

Powershell set-transportrule

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ArnoneAnthony-5113 avatar image
0 Votes"
ArnoneAnthony-5113 answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.