Problem with adding additional SMTP addresses for mail-enabled users with powershell

Answered Problem with adding additional SMTP addresses for mail-enabled users with powershell

  • Friday, February 22, 2013 3:12 PM
     
     

    Hello,

    I have to create Exchange 2010 mail-enabled users for people located on our Sun Iplanet Messaging server. Those users already have account in AD (based on HR number). Also I have to rewrite all their smtp addresses to MEU. I exported all data from Sun LDAP server and created csv:

    User,mail,Proxyaddress,Proxyaddress2,Proxyaddress3,Proxyaddress4,Proxyaddress5,proxyaddress6
    11111111,john.smith@contoso.com,j.smith@contoso.com,jsmi@contoso.com,johnny.smith@contoso.com,jsmith@contoso.com
    11111112,david.brown@contoso.com,d.brown@contoso.com,,,

    As you can see there are different numbers of smtp addresses per user.

    I wrote a script to mail-enable listed users and to setup their email addresses:

    $userobjects = import-csv "test.csv"

    ForEach ( $userobject in $userobjects ) {
    #Assign the content to variables
    $Username = $userobject.user
    $mainaddress = $userobject.mail
    $Proxyaddress1 = $userobject.proxyaddress
    $Proxyaddress2 = $userobject.proxyaddress2
    $Proxyaddress3 = $userobject.proxyaddress3
    $Proxyaddress4 = $userobject.proxyaddress4
    $Proxyaddress5 = $userobject.proxyaddress5
    $Proxyaddress6 = $userobject.proxyaddress6

    $emails = $userobject | foreach {$userobject.proxyaddress,$userobject.proxyaddress2,$userobject.proxyaddress3,$userobject.proxyaddress4,$userobject.proxyaddress5,$userobject.proxyaddress6} 

    enable-MailUser -identity $Username -ExternalEmailAddress $mainaddress
    set-mailuser $username -EmailAddressPolicyEnabled $false
    set-mailuser $username -emailaddresses $emails

    }

    Problem is with those different numbers of addresses because Powershell says ""MultiValuedProperty collections cannot contain null values" for everyone who don't have 6 addresses.

    Is there any method to skip null values while importing csv or to skip nulls while passing it to $emails ?

    Maciek Mystkowski


All Replies

  • Friday, February 22, 2013 4:29 PM
    Moderator
     
     Answered Has Code

    Try this:

    $csv = Import-Csv C:\users\Administrator\Desktop\TechNetUserSample.csv
    
    foreach ($record in $csv) {
    
        if ($record.proxyaddress2 -like '*@*' -ne $true) {
        $record.proxyaddress2 = $record.proxyaddress
        }
        
        if ($record.proxyaddress3 -like '*@*' -ne $true) {
        $record.proxyaddress3 = $record.proxyaddress
        }
        
        if ($record.proxyaddress4 -like '*@*' -ne $true) {
        $record.proxyaddress4 = $record.proxyaddress
        }
        
        if ($record.proxyaddress5 -like '*@*' -ne $true) {
        $record.proxyaddress5 = $record.proxyaddress
        }
        
        if ($record.proxyaddress6 -like '*@*' -ne $true) {
        $record.proxyaddress6 = $record.proxyaddress
        }
    
    }
    
    $csv | % {Set-MailUser $_.user -EmailAddressPolicyEnabled $false -EmailAddresses @{Add=$_.proxyaddress,$_.Proxyaddress2,$_.Proxyaddress3,$_.Proxyaddress4,$_.Proxyaddress5,$_.Proxyaddress6} }
    
    #

    It's probably not the most efficient way to do it, but it seems to work for me.  The real solution would probably be to get an output that uses multivalued attributes instead of null and blank values.

    BTW, note the way I'm updating multivalued attributes:  

    @{Add="<value1>", "<value2>", "<value3>"}



    Mike Crowley | MVP
    My Blog -- Planet Technologies






  • Friday, February 22, 2013 8:28 PM
     
     Answered Has Code

    This is more direct and it is also more PowerShell-like instead of being more VBScript-like. (uses pipeline)

    Import-Csv users.csv |
        ForEach-Object{
            Write-Host $_.user
            enable-MailUser -identity $_.user -ExternalEmailAddress $_.mail
            set-mailuser $_.user -EmailAddressPolicyEnabled $false
            $proxyAddresses=@(
                              $_.Proxyaddress,
                              $_.Proxyaddress2,
                              $_.Proxyaddress3,
                              $_.Proxyaddress4,
                              $_.Proxyaddress5,
                              $_.proxyaddress6
                             ) |?{$_}
           set-mailuser $_.name -emailaddresses $proxyAddresses
        }


    ¯\_(ツ)_/¯



  • Friday, February 22, 2013 9:44 PM
     
      Has Code

    Thanks a lot both of you,

    I tried Jrv method but it only created MEU and then showed twice:

    Cannot bind argument to parameter 'Identity' because it is null.
        + CategoryInfo          : InvalidData: (:) [Set-MailUser], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Set-MailUser

    So since I was also fighting with it while writing my scritpt (thats why it looks like this) I just modifed mine with Jrv solution and it working:

    $userobjects = import-csv "test.csv"
    
    ForEach ( $userobject in $userobjects ) {
    #Assign the content to variables
    $Username = $userobject.user
    $mainaddress = $userobject.mail
    #$Proxyaddress1 = $userobject.proxyaddress
    #$Proxyaddress2 = $userobject.proxyaddress2
    #$Proxyaddress3 = $userobject.proxyaddress3
    #$Proxyaddress4 = $userobject.proxyaddress4
    #$Proxyaddress5 = $userobject.proxyaddress5
    #$Proxyaddress6 = $userobject.proxyaddress6
    
    $proxyAddresses=@(
                              $userobject.proxyaddress,
                              $userobject.proxyaddress2,
                              $userobject.proxyaddress3,
                              $userobject.proxyaddress4,
                              $userobject.proxyaddress5,
                              $userobject.proxyaddress6
                             ) |?{$_}
    
    
    enable-MailUser -identity $Username -ExternalEmailAddress $mainaddress
    set-mailuser $username -EmailAddressPolicyEnabled $false
    set-mailuser $username -emailaddresses $proxyAddresses
    }

    Once more thanks a lot for help

    Maciek Mystkowski

  • Friday, February 22, 2013 9:52 PM
     
     

    now I see what was the problem with Jrv script - $_.name should be $_.user when using my csv file so Jrv version is totally working ;).

    Maciek Mystkowski

  • Friday, February 22, 2013 11:02 PM
     
     

    now I see what was the problem with Jrv script - $_.name should be $_.user when using my csv file so Jrv version is totally working ;).

    Maciek Mystkowski


    I fixed that after I posted but saw the issue.  I see the fix got lost. I am going to try to fix it again.

    ¯\_(ツ)_/¯

  • Friday, February 22, 2013 11:06 PM
     
     

    You should not get into the habit of reassigning values constantly.  It creates opportunity for error and makes the code harder to understand.  A programmer would never get far if he/she wrote programs like that.

    Learn to use the native system.  It is more efficient and easier for technicians to understand.  Do not write code for non-technical consumption.  It will be your undoing.


    ¯\_(ツ)_/¯

  • Friday, February 22, 2013 11:23 PM
     
     

    Thanks for suggestions, I know my Powershell knowledge and skills are not impressive but i'm trying to improve :). You're right about reassigning but i was messing around with "Cannot bind argument to parameter 'Identity' because it is null."  in almost every place of this script and it brought me to such a mess. Probably I made some stupid mistake in my early tries and and later fixed this without even knowing about it :).

    Maciek Mystkowski