Page 1 of 1

SMTP-Addresses Exchange

Posted: Mon Jul 23, 2018 6:38 am
by bhnuser
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?

Re: SMTP-Addresses Exchange

Posted: Mon Jul 23, 2018 7:03 am
by jvierra
I think the following would work better:

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

Re: SMTP-Addresses Exchange

Posted: Wed Jul 25, 2018 1:53 am
by bhnuser
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?

Re: SMTP-Addresses Exchange

Posted: Wed Jul 25, 2018 2:47 am
by jvierra

Code: Select all

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

Re: SMTP-Addresses Exchange

Posted: Wed Jul 25, 2018 6:02 am
by bhnuser
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:",""}}

Re: SMTP-Addresses Exchange

Posted: Wed Jul 25, 2018 5:20 pm
by ITEngineer
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, ...

Re: SMTP-Addresses Exchange

Posted: Thu Jul 26, 2018 5:53 am
by bhnuser
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:",""}}

Re: SMTP-Addresses Exchange

Posted: Thu Jul 26, 2018 7:21 am
by ITEngineer
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.

Re: SMTP-Addresses Exchange

Posted: Thu Jul 26, 2018 7:28 am
by jvierra
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

Re: SMTP-Addresses Exchange

Posted: Thu Jul 26, 2018 7:31 am
by jvierra
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.