locked
Script to set ExternalEmailAddress RRS feed

  • Question

  • 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)
     
    }


    Wednesday, April 27, 2011 9:15 PM

Answers

  • 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!






    • Proposed as answer by Mike Crowley Thursday, April 28, 2011 1:16 AM
    • Marked as answer by The_Messenger Thursday, April 28, 2011 1:25 PM
    Thursday, April 28, 2011 1:14 AM
  • 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!





    • Proposed as answer by Mike Crowley Thursday, April 28, 2011 2:31 PM
    • Marked as answer by The_Messenger Thursday, April 28, 2011 2:34 PM
    Thursday, April 28, 2011 2:29 PM

All replies

  • $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:

    1. $contact is the whole contact object.  you may want to use the $contact.alias attribute.
    2. 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!

    Thursday, April 28, 2011 12:36 AM
  • 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!






    • Proposed as answer by Mike Crowley Thursday, April 28, 2011 1:16 AM
    • Marked as answer by The_Messenger Thursday, April 28, 2011 1:25 PM
    Thursday, April 28, 2011 1:14 AM
  • 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


    Thursday, April 28, 2011 1:28 PM
  • *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!


    Thursday, April 28, 2011 1:33 PM
  • 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.

    Thursday, April 28, 2011 1:36 PM
  • 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!

    Thursday, April 28, 2011 1:52 PM
  • on 2nd thought, just add it to the string above.

    Mike Crowley
    Check out My Blog!

    Thursday, April 28, 2011 2:00 PM
  • 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?

    Thursday, April 28, 2011 2:07 PM
  • 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.

    Thursday, April 28, 2011 2:23 PM
  • 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!





    • Proposed as answer by Mike Crowley Thursday, April 28, 2011 2:31 PM
    • Marked as answer by The_Messenger Thursday, April 28, 2011 2:34 PM
    Thursday, April 28, 2011 2:29 PM
  • 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!

    Thursday, April 28, 2011 2:35 PM
  • No problem.  The learning goes both ways.  :)

     

    What are you looking to do with them (x400)?



    Mike Crowley
    Check out My Blog!

    Thursday, April 28, 2011 2:37 PM
  • Nothing, I was just asking to learn. Could actually create an emai address policy, based on an OU. 

    Thursday, April 28, 2011 2:49 PM
  • 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!

    Thursday, April 28, 2011 2:55 PM
  • Thanks Mike!  I appreciate the help.
    Thursday, April 28, 2011 3:04 PM