Solved

Get all SMTP addresses for a specific domain

Posted on 2014-12-10
6
209 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 70

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 70

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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

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 70

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

Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In-place Upgrading Dirsync to Azure AD Connect
A list of top three free exchange EDB viewers that helps the user to extract a mailbox from an unmounted .edb file and get a clear preview of all emails & other items with just a single click on mailboxes.
In this video we show how to create an Accepted Domain 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 Mail Flow >> Ac…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Suggested Courses

615 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