Script to set ExternalEmailAddress
		
	Developers have a sharepoint site setup that creates mailenabled contacts but the email address they have set is the internal server address.  So, I have an OU with 100 or so contacts that I would like to set "externalemailaddress" for.  Here is
 one of my attemps...  I have commented out another attempt.  the script is not setting the individual contact alias for the email alias.
 
#Set the external email address for the entered OU to 
alias@dashport.domain.com
#Get all contacts in the OU
$contacts=get-mailcontact -organizationalunit "Ou/Operations/TestDashEName"
foreach ($contact in $contacts)
{
#set emailaddresspolicy to false
$contact | set-mailcontact -emailaddresspolicyenabled $false  (this part of the script works fine)
#assign the email domain to each alias  (here the script is not pulling in the $contact variable for either try)
set-mailcontact $contact -externalemailaddress 
$contact@domain.com
#$contact | set-mailcontact -externalemailaddress 
$contact.alias@domain.com
#verify setting
#$contact | get-Mailcontact | select-object Name, ExternalEmailAddress  (this runs fine if I remove the middle section)
 
}		
				April 27th, 2011 5:20pm
			$contact@domain.com doesn't tell the pipeline that you're trying to add the contact's alias with "@domain.com".  There are two problems:
$contact is the whole contact object.  you may want to use the $contact.alias attribute.
You need to break apart the static text (@domain.com) with your variable  See what I did here (http://wp.me/pAAoj-8m) with UPNs.  you need to use the same concept.  I'll work this up for you and post later
 tonight. 
Mike Crowley
Check out My Blog!		
				Free Windows Admin Tool Kit Click here and download it now
					April 27th, 2011 8:37pm
			This should work for Exchange 2007:
 
Get-MailContact -OrganizationalUnit 'demolab.local/DemoLab Contacts' | foreach {($NewAddress = $_.alias + "@domain.com"); Set-MailContact $_.identity -ExternalEmailAddress $NewAddress }
 
And this for Exchange 2010:
 
$Contacts = Get-MailContact -OrganizationalUnit 'demolab.local/DemoLab Contacts'
$contacts | foreach {($NewAddress = $_.alias + "@NewDomain.Com"); Set-MailContact $_.identity -ExternalEmailAdd
ress $NewAddress }
 
The reason Exchange 2010 requires 2 lines is described 
here.
 
Mike Crowley
Check out My Blog!		
				April 27th, 2011 9:15pm
			Thanks Mark!  This works great.  I am running 2007
 
So, what exactly does "$_.identity" and $NewAddress tell the script?
 
I now have a new request for the same OU.  I need to add 2 more addresses to each mailcontact in the OU.  I need the +=emailaddresses commandlet to keep the existing one.
Thanks for  your help and instruction		
				Free Windows Admin Tool Kit Click here and download it now
					April 28th, 2011 9:30am
			*Mike, but I'll forgive you.  :)
 
Question 1)
For each contact we encounter, we run two commands:
build a new variable called $newaddress with the contact's alias + @domain.com
stamp the contact's -externalemailaddress with the current value of the $newaddress variable
The second bullet requires us to define who we're working with.  $_.identity uses the current identity of the contact.
 
Question 2)
You want to add an additional proxy address for the existing contacts, while preserving the work we did with the above script?  Confirm this so that I'm sure I understand.
Mike Crowley
Check out My Blog!		
				April 28th, 2011 9:35am
			LOL sorry.  I typed it right but I had left the window open over night so the site wouldn't submit.  Don't know how I got Mark on the second time.  :)
Yes that is correct.  I thought I had tested the -emailaddresses cmdlet to add but this still replaces one of the addresses.  They want to add an internal address that uses .org, and an address that is @servermachinename and keep the
 external one we just added that uses .com.		
				Free Windows Admin Tool Kit Click here and download it now
					April 28th, 2011 9:38am
			There are a few ways to do that.  Here is one: 
http://help.outlook.com/en-us/140/cc967281.aspx
Mike Crowley
Check out My Blog!		
				April 28th, 2011 9:54am
			on 2nd thought, just add it to the string above.
Mike Crowley
Check out My Blog!		
				Free Windows Admin Tool Kit Click here and download it now
					April 28th, 2011 10:02am
			Mike,   
will  $users = Get-Mailbox
work the same as
$users = Get-Mailcontact -organizationalunit "organizationalunit"
I was following till you said to add it to the string above - how would I add it?		
				April 28th, 2011 10:09am
			This appears to work using get-mailbox but I am getting 'cannot conver the value "System.Oject[]" ...' error
 
Get-Mailcontact -OrganizationalUnit 'Ou/name' | foreach { 
$user = (Get-Mailcontact $_.Name).Name 
$email1= "$user@sub.domain.org"
$email2= "$user@servername.hntb.org"
$_.emailAddresses += ($email1,$email2) 
Set-Mailcontact $_ -emailAddresses $_.emailAddresses 
}
######################################
I did try editing your original solution from
 Set-MailContact 
$_.identity -ExternalEmailAddress 
$NewAddress }
 to
Set-MailContact $_.identity -EmailAddresses $NewAddress}
I thought I would just run it for each address.   This works but it changes the address listings instead of adding an address.		
				Free Windows Admin Tool Kit Click here and download it now
					April 28th, 2011 10:25am
			I did something similiar.  This incorporates the previously posted logic as well.  You could save this as a .ps1 file:
 
 
$contacts = Get-MailContact -OrganizationalUnit 'demolab.local/DemoLab Contacts'
$contacts | foreach {
$OutsideAddress = ($_.alias + "@OutsideDomain.com")
$ProxyAddress1 = ($_.alias + "@InsideDomain1.com")
$ProxyAddress2 = ($_.alias + "@InsideDomain2.com")
Set-MailContact -Identity $_.identity -ExternalEmailAddress $OutsideAddress -EmailAddresses $OutsideAddress, $ProxyAddress1, $ProxyAddress2
}
 
(This will work in Exchange 2007 or 2010)
 
Mike Crowley
Check out My Blog!		
				April 28th, 2011 10:31am
			Excellent!  Thanks, this will give me the solution and it helps much in (scripting-learning)
A final question, just wondering on this one.  What would be required for an x400 address?
 
Thanks for you help and time!		
				Free Windows Admin Tool Kit Click here and download it now
					April 28th, 2011 10:37am
			No problem.  The learning goes both ways.  :)
 
What are you looking to do with them (x400)?
Mike Crowley
Check out My Blog!		
				April 28th, 2011 10:39am
			Nothing, I was just asking to learn. Could actually create an emai address policy, based on an OU.		
				Free Windows Admin Tool Kit Click here and download it now
					April 28th, 2011 10:51am
			Well, you can add another variable and work with them too.  But Exchange 2007 doesn't need x400 anyway.  If you still need them for Exchange 2003, I'd use the EAP as you suggested. 
 
Good Luck!
Mike Crowley
Check out My Blog!		
				April 28th, 2011 10:57am
			Thanks Mike!  I appreciate the help.		
				Free Windows Admin Tool Kit Click here and download it now
					April 28th, 2011 11:06am
			

