SMTP-Addresses Exchange

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
bhnuser
Posts: 42
Joined: Fri Apr 06, 2018 7:35 am

SMTP-Addresses Exchange

Post by bhnuser » Mon Jul 23, 2018 6:38 am

Hallo everybody,

i have an issue with my Admin-Tool. For your information: the script was already running on my system but after few weeks i get some problems.
I start a session with our Exchange-Server and collect from there some information about the user. Now i want to get all Smtp adresses, split in primary- and normal emailaddresses. If i start the snippet below i get only the primaryemailaddress. If i start the snippet localy on the server i get both informations.
The snippet is: Get-Mailbox -Identity $selectedUser.SamAccountName -ResultSize Unlimited | Select-Object PrimarySmtpAddress, @{ Name = "EmailAddresses"; Expression = { $_.EmailAddresses | Where-Object { $_.PrefixString -ceq "smtp" } | ForEach-Object { $_.SmtpAddress } } }

Can someone help me there? I dont know why the server didnt return me both informations. Or is there a better way to get the smtpaddresses?

User avatar
jvierra
Posts: 13617
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: SMTP-Addresses Exchange

Post by jvierra » Mon Jul 23, 2018 7:03 am

I think the following would work better:

Get-Mailbox $selectedUser.SamAccountName |
Select-Object PrimarySmtpAddress, @{n='EmailAddresses';e={ $_.EmailAddresses -join '|'}}

User avatar
bhnuser
Posts: 42
Joined: Fri Apr 06, 2018 7:35 am

Re: SMTP-Addresses Exchange

Post by bhnuser » Wed Jul 25, 2018 1:53 am

That sounds very good. Now i get all information like x500, MRS and SMTP.
Can you tell me how i can filter only the SMTP-Address?

User avatar
jvierra
Posts: 13617
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: SMTP-Addresses Exchange

Post by jvierra » Wed Jul 25, 2018 2:47 am

Code: Select all

$select = @(
    'PrimarySmtpAddress', 
    @{n='EmailAddresses';e={$_.EmailAddresses.Where({$_ -match 'smtp:' }) -join '|'}}
)
Get-Mailbox $selectedUser.SamAccountName | select $select

User avatar
bhnuser
Posts: 42
Joined: Fri Apr 06, 2018 7:35 am

Re: SMTP-Addresses Exchange

Post by bhnuser » Wed Jul 25, 2018 6:02 am

I thank you very much.
Just some little modifies on my code:

Code: Select all

Get-Mailbox $selectedUser.SamAccountName | select PrimarySmtpAddress, @{Name='EmailAddresses'; Expression={$_.EmailAddresses.Where({$_ -like 'smtp:*' }) -replace "smtp:",""}}

User avatar
ITEngineer
Posts: 145
Joined: Wed Oct 12, 2011 10:52 am

Re: SMTP-Addresses Exchange

Post by ITEngineer » Wed Jul 25, 2018 5:20 pm

Thanks for posting such great script :)
So is it possible to put comma "," in the EmailAddresses column result?

EmailAddresses
Email1@domain.com, Email2@domain.com, Email3@domain.com, ...
/* IT Engineer */

User avatar
bhnuser
Posts: 42
Joined: Fri Apr 06, 2018 7:35 am

Re: SMTP-Addresses Exchange

Post by bhnuser » Thu Jul 26, 2018 5:53 am

Yes it's possible.
But i dont know which code you use...
If you use my last posted code you will recive the EmailAddresses with "," separated instand of "|"

Use this:

Code: Select all

Get-Mailbox $selectedUser.SamAccountName | select PrimarySmtpAddress, @{Name='EmailAddresses'; Expression={$_.EmailAddresses.Where({$_ -like 'smtp:*' }) -replace "smtp:",""}}

User avatar
ITEngineer
Posts: 145
Joined: Wed Oct 12, 2011 10:52 am

Re: SMTP-Addresses Exchange

Post by ITEngineer » Thu Jul 26, 2018 7:21 am

bhnuser wrote:
Thu Jul 26, 2018 5:53 am
Yes it's possible.
But i dont know which code you use...
If you use my last posted code you will recive the EmailAddresses with "," separated instand of "|"

Use this:

Code: Select all

Get-Mailbox $selectedUser.SamAccountName | select PrimarySmtpAddress, @{Name='EmailAddresses'; Expression={$_.EmailAddresses.Where({$_ -like 'smtp:*' }) -replace "smtp:",""}}
Yes, I've used the above code.
When I open the result.CSV it is showing with blank space as the delimiter not comma.
/* IT Engineer */

User avatar
jvierra
Posts: 13617
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: SMTP-Addresses Exchange

Post by jvierra » Thu Jul 26, 2018 7:28 am

I had it set up correctly but the OP decided to make odd changes that make it behave badly.
Here. This makes it a comma.

Code: Select all

$select = @(
    'PrimarySmtpAddress', 
    @{n='EmailAddresses';e={$_.EmailAddresses.Where({$_ -match 'smtp:' }) -join ','}}
)
Get-Mailbox $selectedUser.SamAccountName | select $select

User avatar
jvierra
Posts: 13617
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: SMTP-Addresses Exchange

Post by jvierra » Thu Jul 26, 2018 7:31 am

To easily strip the "smtp:" just add this:

Code: Select all

$select = @(
    'PrimarySmtpAddress', 
    @{n='EmailAddresses';e={$_.EmailAddresses.Where({$_ -match 'smtp:' } -replace 'smtp:' -join ','}}
)
Get-Mailbox  | select $select
Notice that "replace" does not require an empty string to remove the matched value.

Locked