header1   header
header
header : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
IMPORTANT: PowerShellCommunity.org is moving! - Wednesday, August 15, 2012

PowerShellCommunity.org is moving!  This community software, and the hardware that it sits on, are no longer serving the purposes of this community.  As a result, we have decided to move this community to a new home at PowerShell.org.  PowerShell.org is already up and running with the new community software and in its new location, so please post any new questions that you have on the forums over there instead of posting them on this site.  We've already started getting some great questions from members of the community over there so please, come on over and join us!

While we are going through this transition, this site will remain up for the short term.  New posts may no longer be created on these forums, however replies to existing posts are allowed so that users who posted questions don't have to re-post the same question on the new site.

[UPDATE 28/02/2013] New user registration has been disabled and forums have now been switched to read-only, including for existing posts since all threads that were started should now be completed. If you have a question about content on this site or about PowerShell in general, head over to PowerShell.org and ask it there where there are people actively using the site and answering questions.

If you have any questions, please let us know on the PowerShell.org site.

Thank you,

Kirk "Poshoholic" Munro

 
Add multiple SMTP addresses to users
Last Post 16 Feb 2012 12:52 AM by Decle. 3 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
DecleUser is Offline
New Member
New Member
Posts:3
Avatar

--
15 Feb 2012 03:22 AM
    We have ca. 170 users who will be migrated to a new domain.
    Their new accounts will only have one SMTP, decided by the default policy.

    We have to add several of their old SMTP's to each user account.
    Anyone has some tips, or perhaps a ready to use command due to similair experiences?

    Using exchange 2007 sp2.
    ArmentpauUser is Offline
    New Member
    New Member
    Posts:2
    Avatar

    --
    15 Feb 2012 09:17 PM
    NOTE: I just copied my code over.  I have not edited the script to be more "global" and it still references absolute paths which I use for testing/logging - so you will need to modify the scripts some.  I love helping - but need to leave a little bit of fun for you :D


    here is a quick bit of code i have used:

    <div>$email_temp_holder = get-mailbox $email
    $email_temp_holder.emailaddresses += $new_email
    set-mailbox $email -emailAddresses $email_temp_holder.emailaddresses</div><div>


    The full code I use looks like this (this assumes I know the current email addresses)

     
    #empties resulting log/text file
    "">c:\Powershell\Paul_Test\results_file.txt
    cls
    #imports the text file containing the existing email addresses, one per line - its a text file but treated as a csv file.  only one column with a heading of email
    $list = Import-Csv "c:\Powershell\Paul_Test\concur_import.txt"
    
    foreach($item in $list)
    {
        $email = $item.email
            #gets the linked master account to generate the appropriate username of the personal smtp address
        $user = (Get-Mailbox $email | select linkedmasteraccount).linkedmasteraccount
            #determines if a linked master is found or not
        if((Get-Mailbox $email | select linkedmasteraccount).linkedmasteraccount)
        {
            #generate the new prefix - before the @
                    $new_pre = (((get-qaduser $user -service vcpidc01.vcpi.com | select firstname).firstname).substring(0,1)) + ((Get-QADUser $user -Service vcpidc01.vcpi.com | select lastname).lastname)
            Disconnect-QADService
                    
            $user_temp = (Get-Mailbox $email | select samaccountname).samaccountname
            $user_info = ((Get-QADUser $user_temp | select UserPrincipalName).userprincipalname.split("@"))[1]
            $new_email = $new_pre+"@"+$user_info
            Write-Host $new_email
            $flag = 0
            $suffix = 0
            do{
                            #we need to check if the email acocunt we want to create exists or not first before continuing
                if(Get-Mailbox $new_email|select name)
                {
                    Write-Host "Email found"
                    $suffix +=1
                    $new_email = $new_pre+$suffix+"@"+$user_info
                    Write-Host $suffix
                }
                else
                {
                    $flag = 1
                    Write-Host "Email not found - setting the account up"
                    $email_temp_holder = get-mailbox $email
                    if($suffix -eq 0)
                    {    
                        $new_email = $new_pre+"@"+$user_info
                    }
                    else
                    {    
                        $new_email = $new_pre+$suffix+"@"+$user_info
                    }
                                    #we turn off the emailadddresspolicy for other reasons 
                    get-mailbox $email | set-mailbox -EmailAddressPolicyEnabled $false
                    $email_addresses = get-mailbox $email | select -expand EmailAddresses | %{$_.SmtpAddress}
                    $email_temp_holder.emailaddresses += $new_email
                    set-mailbox $email -emailAddresses $email_temp_holder.emailaddresses
                    $new_email >> c:\Powershell\Paul_Test\results_file.txt
                }
            }while($flag -eq 0)
        }
    }
    


    For something like what you want to do (adding existing email addresses to the smtp address) I would modify my code as follows:

    
    "">c:\Powershell\Paul_Test\results_file.txt
    cls
    $list = Import-Csv "c:\Powershell\Paul_Test\concur_import_2.csv"
    foreach($item in $list)
    {
        $email = ""
        $new_smtp = ""
        $email_temp_holder = ""
        
        $email = $item.email
        $new_smtp = $item.new_smtp.split(";")    
        $email_temp_holder = get-mailbox $email
        foreach($item_2 in $new_smtp)
        {    
            $email_temp_holder.emailaddresses += $item_2
        }
        #get-mailbox $email | set-mailbox -EmailAddressPolicyEnabled $false
        set-mailbox $email -emailAddresses $email_temp_holder.emailaddresses
        
    }
    
     


    You can of course add output to the screen/file as needed - but this will add those secondary smtp addresses for you. The csv file would be setup with the columns of: email and new_smtp. The column new_smtp can contain multiple email addresses separated by a ; so for the above snippet I used the below csv file to test my code out:

    
    
    email,new_smtp
    pdearment@test.com,pdtest@test.com;pdtest_2@test.com;pdtest3@testcorp.com
     
    DecleUser is Offline
    New Member
    New Member
    Posts:3
    Avatar

    --
    15 Feb 2012 11:21 PM
    Thanks a lot for sharing your script, i'll dive into it today and update this thread with the outcome later on.

    Much appreciated! :)
    DecleUser is Offline
    New Member
    New Member
    Posts:3
    Avatar

    --
    16 Feb 2012 12:52 AM
    It works perfectly!
    No modifications required at all. (thank god, since I am terrible at scripting..)

    Thanks again Paul!
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Many thanks to our original sponsors: Quest Software • SAPIEN Technologies • Compellent • Microsoft footer
    footer   footer