SMTP-Addresses Exchange

Ask your 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.
This topic is 5 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
bhnuser
Posts: 48
Last visit: Tue Nov 21, 2023 10:33 pm

SMTP-Addresses Exchange

Post 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?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: SMTP-Addresses Exchange

Post by jvierra »

I think the following would work better:

Get-Mailbox $selectedUser.SamAccountName |
Select-Object PrimarySmtpAddress, @{n='EmailAddresses';e={ $_.EmailAddresses -join '|'}}
User avatar
bhnuser
Posts: 48
Last visit: Tue Nov 21, 2023 10:33 pm

Re: SMTP-Addresses Exchange

Post 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?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: SMTP-Addresses Exchange

Post by jvierra »

Code: Select all

$select = @(
    'PrimarySmtpAddress', 
    @{n='EmailAddresses';e={$_.EmailAddresses.Where({$_ -match 'smtp:' }) -join '|'}}
)
Get-Mailbox $selectedUser.SamAccountName | select $select
User avatar
bhnuser
Posts: 48
Last visit: Tue Nov 21, 2023 10:33 pm

Re: SMTP-Addresses Exchange

Post 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:",""}}
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Re: SMTP-Addresses Exchange

Post 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, ...
/* IT Engineer */
User avatar
bhnuser
Posts: 48
Last visit: Tue Nov 21, 2023 10:33 pm

Re: SMTP-Addresses Exchange

Post 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:",""}}
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Re: SMTP-Addresses Exchange

Post 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.
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: SMTP-Addresses Exchange

Post 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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: SMTP-Addresses Exchange

Post 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.
This topic is 5 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked