header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

Exchange 2007 Replace Primary SMTP address
Last Post 11 Jul 2008 03:58 PM by KarlMitschke. 9 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
KarlMitschkeUser is Offline
Basic Member
Basic Member
Posts:161

--
28 Mar 2008 09:01 PM  

I have a web page that we allow ou administrators to use to modify mailboxes. One task allows them to change an email address.
This comes in handy when a user gets married or divorced, or changes his or her name, and in those cases, usually we would like to keep the old email addresses so mail to those old email addresses still goes to the mailbox.
So, I created a script to add an email address, and set it as the primary address, and life was good....for about 10 minutes, until administrators started whining about wanting to create a mailbox, change the default address, and NOT have the old address available.
It turns out, that due to latency, you can't simply do:

set-mailbox -identity  $user -PrimarySmtpAddress $newaddressstring -EmailAddressPolicyEnabled $false
$mailbox = Get-Mailbox -identity $user
$mailbox.EmailAddresses | foreach { if (!$_.IsPrimaryAddress -and ($_.PrefixString -eq 'SMTP')) {$mailbox.EmailAddresses -= $_}}
set-mailbox -identity $user -EmailAddresses $mailbox.EmailAddresses


So, I came up with this:

$newaddressstring = "karlmitschketest@testmail.com"
$user = "ktester"
set-mailbox -identity  $user -PrimarySmtpAddress $newaddressstring -EmailAddressPolicyEnabled $false
do{$address = get-mailbox -identity $user |select PrimarySmtpAddress}while ($address.PrimarySmtpAddress.ToString().ToLower() -ne $newaddressstring)
$mailbox = Get-Mailbox -identity $user
$mailbox.EmailAddresses | foreach { if (!$_.IsPrimaryAddress -and ($_.PrefixString -eq 'SMTP')) {$mailbox.EmailAddresses -= $_}}
set-mailbox -identity $user -EmailAddresses $mailbox.EmailAddresses

This works in the EMS, and I use she same basic function in my C# aspx page.

Karl

haggardUser is Offline
New Member
New Member
Posts:7

--
21 May 2008 03:08 PM  

I have a question.  This script is "close" to what I need, but not exactly.  The script above will change the primary smtp address, but it wipes out any secondary smtp addresses on the object.  Here is what I need:

  • Update/replace primary smtp address with a new value
  • After updating, script does not modify secondary smtp addresses
  • Script purges original primary smtp address and does not add it as a secondary address

I modified the above script to accept user input, but I need the necessary mods to make this work for my needs.  Here's what I'm currently using based on the above script:

 

 

 

$newaddressstring =  Read-Host "Please enter the new primary SMTP"
$user =  Read-Host "Please enter the users login ID "
set-mailbox -identity  $user -PrimarySmtpAddress $newaddressstring -EmailAddressPolicyEnabled $false
do{$address = get-mailbox -identity $user |select PrimarySmtpAddress}while ($address.PrimarySmtpAddress.ToString().ToLower() -ne $newaddressstring)
$mailbox = Get-Mailbox -identity $user
$mailbox.EmailAddresses | foreach { if (!$_.IsPrimaryAddress -and ($_.PrefixString -eq 'SMTP')) {$mailbox.EmailAddresses -= $_}}
set-mailbox -identity $user -EmailAddresses $mailbox.EmailAddresses

KarlMitschkeUser is Offline
Basic Member
Basic Member
Posts:161

--
21 May 2008 03:12 PM  
This should work:


$newaddressstring = "karlmitschketest@testmail.com"
$user = "ktester"
set-mailbox -identity  $user -PrimarySmtpAddress $newaddressstring -EmailAddressPolicyEnabled $false
haggardUser is Offline
New Member
New Member
Posts:7

--
21 May 2008 03:15 PM  

KarlMitschke,

Please bare with me, as I'm still a powershell rookie.  It looks to me like the changes you suggested are the same as what I'm currently using in the script.  Am I wrong?  by the way, this forum is great.  thanks!

KarlMitschkeUser is Offline
Basic Member
Basic Member
Posts:161

--
21 May 2008 03:28 PM  

I think I may be misunderstanding what you want;

Say you have an account Karl Tester;

His current Primary SMTP address is "ktester@nowhere.com"

Does he have a secondary SMTP address of say samaccountname@nowhere.com. and you want to change ktester, but leave samaccountname@nowhere.com

Karl

haggardUser is Offline
New Member
New Member
Posts:7

--
21 May 2008 03:41 PM  

Ok...  Let me clarify. 

 


This is what the test account looks like before I run the script. 

[PS] W:\powershell>Get-Mailbox -identity test.cortez@ucdenver.edu | fl proxyaddresses,emailaddresses,displayname


EmailAddresses : {smtp:test3@ucdenver.edu, smtp:test2@ucdenver.edu, smtp:test1@ucdenver.edu, SMTP:Test.Cortez@ucdenver.edu}
DisplayName    : Test, Cortez

Now, lets say that I want to change the primary smtp from test.cortez@ucdenver.edu to tester.cortez@ucdenver.edu, leaving the other smtp addresses (smtp:test3@ucdenver.edu, smtp:test2@ucdenver.edu, smtp:test1@ucdenver.edu) in tact

Here is what happens currently with the script (called primaryaddresschanger.ps1) referenced above:

[PS] W:\powershell>.\primaryaddchanger.ps1
Please enter the new primary SMTP: tester.cortez@ucdenver.edu
Please enter the users login ID : testcortez

 

This shows the updated smtp information on the object after the script modified the object properties:

[PS] W:\powershell>Get-Mailbox -identity testcortez | fl proxyaddresses,emailaddresses,displayname


EmailAddresses : {SMTP:tester.cortez@ucdenver.edu}
DisplayName    : Test, Cortez

This shows that once the primary SMTP has been updated, the secondary smtp addresses are no longer on the account, the script wipes them out.  I would like to update the address, and retain all existing secondary smtp addresses.  I hope this is more clear.  If it is not, please let me know what else I can clarify..  THANKS.... 

 

KarlMitschkeUser is Offline
Basic Member
Basic Member
Posts:161

--
21 May 2008 03:47 PM  
Haggard;

Try this:

$newaddressstring =  Read-Host "Please enter the new primary SMTP"
$user =  Read-Host "Please enter the users login ID "
$oldaddress = get-mailbox -identity  $user |select PrimarySmtpAddress
set-mailbox -identity  $user -PrimarySmtpAddress $newaddressstring -EmailAddressPolicyEnabled $false
do{$address = get-mailbox -identity $user |select PrimarySmtpAddress}while ($address.PrimarySmtpAddress.ToString().ToLower() -ne $newaddressstring)
$mailbox = Get-Mailbox -identity $user
$mailbox.EmailAddresses -= $oldaddress.PrimarySmtpAddress.ToString()
set-mailbox -identity $user -EmailAddresses $mailbox.EmailAddresses
haggardUser is Offline
New Member
New Member
Posts:7

--
21 May 2008 04:05 PM  
This appears to work great... I'll do some more testing and let you know.. THANKS!!
kdeshongUser is Offline
New Member
New Member
Posts:1

--
10 Jul 2008 09:47 PM  

I will one up you. I need to replace the primary SMTP address on an entire OU. What adjustments do I need to make to the script to pull all addresses from an OU instead of addressing just one account.

Thanks

(scripting rookie :-))

KarlMitschkeUser is Offline
Basic Member
Basic Member
Posts:161

--
11 Jul 2008 03:58 PM  
Hello;

Are you trying to change the domain name of all the mailboxes, or the users name?

I'd probaly do this via a recipient policy.

Karl
You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer