Link to home
Start Free TrialLog in
Avatar of georgedschneider
georgedschneiderFlag for United States of America

asked on

Get all SMTP addresses for a specific domain

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.
Avatar of Qlemo
Qlemo
Flag of Germany image

Your question is unclear.  Do you want a listing of all matching addresses (primary or secondary), without the SMTP prefix?
Avatar of georgedschneider

ASKER

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.
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

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

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

ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial