Solved

Get all SMTP addresses for a specific domain

Posted on 2014-12-10
6
155 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 69

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 69

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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 69

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Marketers need statistics and metrics like everybody else needs oxygen. In this article we explain how to enable marketing campaign statistics for Microsoft Exchange mail.
This article explains how to install and use the NTBackup utility that comes with Windows Server.
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

815 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

11 Experts available now in Live!

Get 1:1 Help Now