Solved

Get all SMTP addresses for a specific domain

Posted on 2014-12-10
6
145 Views
Last Modified: 2014-12-30
I'm trying to produce an output of all users with a specific primary email address.  The following script will produce this:

Get-Mailbox -ResultSize unlimited|Where-Object {$_.PrimarySMTPAddress.Domain -eq "domain.com"}|Select @{n='Name';e={$_.displayname}},@{n='Primary Email Address';e={$_.primarysmtpaddress}},@{name='All Email Addresses';e={$_.emailaddresses}}

Open in new window


I'd like to exclude all x500 addresses from the output.  I know using Where-Object {($_ -Like "SMTP:*")} will accomplish this but I can't get it to work with specifying desired domain.

Is there a way to look at both primary and secondary domains like i have in this script for primary domain?

Long term I'd like to remove smtp: so the output is just a listing of addresses.
0
Comment
Question by:georgedschneider
  • 3
  • 3
6 Comments
 
LVL 68

Expert Comment

by:Qlemo
ID: 40493580
Your question is unclear.  Do you want a listing of all matching addresses (primary or secondary), without the SMTP prefix?
0
 

Author Comment

by:georgedschneider
ID: 40494436
In the output I'd like to have smtp: removed from the email addresses output.  I'd like to have the csv export display each address without this to make it a little easier to work with.  I'm trying to exclude x500 addresses as well.
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 40495914
Get-Mailbox -ResultSize unlimited |
  ? {$_.PrimarySMTPAddress.Domain -eq "domain.com"} |
  Select @{n='Name';e={$_.displayname}},
         @{n='Primary Email Address';e={$_.primarysmtpaddress}},
         @{name='All Email Addresses';e={($_.emailaddresses | ? {$_ -like 'SMTP:*'}) -replace 'SMTP:'}}

Open in new window

0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:georgedschneider
ID: 40496646
This works great.  I'm trying to incorporate it in the following script but I can't get the logic with the -replace statement to work.

$(Foreach ($Mailbox in (Get-Mailbox -ResultSize Unlimited)|? {($_.PrimarySMTPAddress.Domain -eq "doamin.com") -or ($_.emailaddresses -like '*domain.com')}){
$Size = Get-MailboxStatistics $Mailbox.Alias | Select @{N="totalitemsizeMB";E={'{0:f2}' -f ($_.TotalItemSize.Value.ToBytes()/1MB)}},itemcount
	New-Object PSObject -Property @{
		Name = $Mailbox.Displayname
		"Primary Email Address" = $Mailbox.Primarysmtpaddress
        "All Email Addresses" = $Mailbox.emailaddresses|? {$_ -like 'SMTP:*'} 
		"Mailbox Size (MB)" = $Size.totalitemsizeMB
        "Item Count" = $Size.itemcount
	}
})|Select Name,"PrimaryEmail Address", "All Email Addresses","Mailbox Size (MB)", "Item Count"|Sort-Object Name

Open in new window

0
 

Author Comment

by:georgedschneider
ID: 40496767
I thought the following logic was what I needed.  This produces the desired results.


$(Foreach ($Mailbox in (Get-Mailbox -ResultSize Unlimited)|? {($_.PrimarySMTPAddress.Domain -eq "domain.com") -or ($_.emailaddresses -like '*domain.com')}){
$Size = Get-MailboxStatistics $Mailbox.Alias | Select @{N="totalitemsizeMB";E={'{0:f2}' -f ($_.TotalItemSize.Value.ToBytes()/1MB)}},itemcount
	New-Object PSObject -Property @{
		Name = $Mailbox.Displayname
		"Primary Email Address" = $Mailbox.Primarysmtpaddress
        "All Email Addresses" = ($Mailbox.emailaddresses|? {$_ -like 'SMTP:*'})-replace 'SMTP:'
		"Mailbox Size (MB)" = $Size.totalitemsizeMB
        "Item Count" = $Size.itemcount
	}
})|Select Name,"Primary Email Address", "All Email Addresses","Mailbox Size (MB)", "Item Count"|Sort-Object Name

Open in new window

.


However if I try to export to CSV using the following code the All email addresses column does not contain the needed information.  It displays system.object[] instead.

$(Foreach ($Mailbox in (Get-Mailbox -ResultSize Unlimited)|? {($_.PrimarySMTPAddress.Domain -eq "domain.com") -or ($_.emailaddresses -like '*domain.com')}){
$Size = Get-MailboxStatistics $Mailbox.Alias | Select @{N="totalitemsizeMB";E={'{0:f2}' -f ($_.TotalItemSize.Value.ToBytes()/1MB)}},itemcount
	New-Object PSObject -Property @{
		Name = $Mailbox.Displayname
		"Primary Email Address" = $Mailbox.Primarysmtpaddress
        "All Email Addresses" = ($Mailbox.emailaddresses|? {$_ -like 'SMTP:*'})-replace 'SMTP:'
		"Mailbox Size (MB)" = $Size.totalitemsizeMB
        "Item Count" = $Size.itemcount
	}
})|Select Name,"Primary Email Address", "All Email Addresses","Mailbox Size (MB)", "Item Count"|Sort-Object Name|export-csv "C:\test.csv" -notypeinformation

Open in new window

0
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
ID: 40498024
With cmdlets potentially generating a lot of data it is usually no good idea to use them in subexpressions (  $(foreach ...) ). You should try to avoid that, and I don't see any reason why you switched from my code example to that.
Get-Mailbox -ResultSize unlimited |
  ? { ($_.PrimarySMTPAddress.Domain -eq "domain.com") -or ($_.emailaddresses -like '*domain.com') } | % {
    $MailBox = $_
    $Size = Get-MailboxStatistics $Mailbox.Alias | Select @{N="totalitemsizeMB";E={'{0:f2}' -f ($_.TotalItemSize.Value.ToBytes()/1MB)}},itemcount
    New-Object PSObject -Property @{
      Name = $Mailbox.Displayname
      "Primary Email Address" = $Mailbox.Primarysmtpaddress
      "All Email Addresses" = ($Mailbox.emailaddresses | ? {$_ -like 'SMTP:*'}) -replace 'SMTP:' -join ';'
      "Mailbox Size (MB)" = $Size.totalitemsizeMB
      "Item Count" = $Size.itemcount
    }
} | Select Name,"Primary Email Address", "All Email Addresses","Mailbox Size (MB)", "Item Count" |
  Sort-Object Name |
  Export-Csv -NoType "C:\test.csv"

Open in new window

The issue with "All EMail Addresses" was that is was a collection of strings; the console output (the implicit out-string called before you see output) tries to enumerate the values (and hence shows them), but other cmdlets do not. So we had to make it a single string containing all data; I chose to use a semicolon as delimiter.
0

Featured Post

Why do Marketing keep bothering you?

Is your marketing department constantly asking for new email signature updates? Are they requesting a different design for every department? Do they need yet another banner added? Don’t let it get you down! There is an easy way to manage all of these requests...

Join & Write a Comment

Check out this infographic on what you need to make a good email signature that will work perfectly for your organization.
Exchange server is not supported in any cloud-hosted platform (other than Azure with Azure Premium Storage).
In this video we show how to create a Contact in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Recipients >> Contact ta…
In this Micro Video tutorial you will learn the basics about Database Availability Groups and How to configure one using a live Exchange Server Environment. The video tutorial explains the basics of the Exchange server Database Availability grou…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now