none
Exchange Powershell - Adding multiple smtp with variable RRS feed

  • Question

  • Good morning,

    I am facing an obstacle and as my knowledge in powershell isn't that advanced, I'm hoping you guys can give me a hand on how I can do this.

    I have a .csv file called Afas01.csv containing AD values which I import in Powershell with Import-Csv.

    I have the value UserPrincipalName which contains a full e-mail address ''testuser@testcompany.com'' and I am able to put this in a new value called mailNickname with removing the ''@testcompany.com'', giving me output ''testuser'' to the field ''mailNickname'' by using the script below:

    Import-Csv D:\Afas01.csv |
    Select *,@{N='mailNickname';E={$_.UserPrincipalName.Split('@')[0]}} |
    Export-Csv D:\Afas02.csv -NoTypeInformation

    This is to create the Alias for the mailbox I will be creating from an existing AD user.

    I also need to add multiple smtp mail addresses and this is turned around, as I do not want to remove the @testcompany.com, but I want to give multiple @ addresses and make the variable fill in the user before the @, so:

    I need to create (where $_.UserPrincipalName is the variable it grabs from the .csv and puts it in a new field so I can add these to Exchange):

    SIP $_.UserPrincipalName@test.nl > field ''sipOne

    SMTP $_.UserPrincipalName@test.nl > field ''smtpOne''

    smtp $_.UserPrincipalName@test.com > field ''smtpTwo''

    smtp $_.UserPrincipalName@test.eu > field ''smtpThree''

    smtp $_.UserPrincipalName@test.onmicrosoft.com > field ''smtpFour''

    Image of an existing user with all the needed mailboxes added below (Censored name and company email 80% due to sensitive company data)

    Thanks!




    Friday, March 18, 2016 7:57 AM

Answers

  • Hi Matthew,

    you can combine strings with variables like this:

    "SMTP:$($_.UserPrincipalName)@test.nl"

    Beware however that UserPrincipalName usually has a domain appendage.

    You'll need to use Set-Mailbox to add those addresses or pass it as part of your Enable-Mailbox call.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Friday, March 18, 2016 8:21 AM
  • Hi Matthew,

    just checked it out, you'll need to use Set-Mailbox anyway - you can't add other addresses with Enable-Mailbox*. You won't need to specify the protocol for PrimarySmtpAddress during Enable-Mailbox, but you WILL need it during Set-Mailbox.

    Cheers,
    Fred

    *Observation made with Exchange 2010, but I doubt this has changed since


    There's no place like 127.0.0.1


    • Edited by FWN Friday, March 18, 2016 8:27 AM
    • Marked as answer by Matthew van Vliet Friday, March 18, 2016 8:52 AM
    Friday, March 18, 2016 8:27 AM
  • Hi Matthew,

    actually, no, that's not how you go about it. In a cmdlet, you can only add each parameter once. For learning how to use Set-Mailbox, check out its online documentation. Specifically the parameter description of the parameter -EmailAddresses.

    You can safe the output of a cmdlet in a variable:

    # Store output in variable
    $user = Get-ADuser "Norman.Normal"
    
    # Reuse information for whatever you want
    $user | Set-ADuser -HomePhone "12345"

    So there is no need to write it to file, just to reimport it again.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Friday, March 18, 2016 8:44 AM

All replies

  • Hi Matthew,

    you can combine strings with variables like this:

    "SMTP:$($_.UserPrincipalName)@test.nl"

    Beware however that UserPrincipalName usually has a domain appendage.

    You'll need to use Set-Mailbox to add those addresses or pass it as part of your Enable-Mailbox call.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Friday, March 18, 2016 8:21 AM
  • Hi. Thanks!

    I also found the parameter -PrimarySmtpAddress:

    Enable-Mailbox -Identity $_mailNickname -PrimarySmtpAddress <SmtpAddress>

    Will this work and if so, will I need to put SMTP: in front of the e-mail when adding this?

    Or will this only work for one smtp address that I'm using and not for the others?


    Friday, March 18, 2016 8:25 AM
  • Hi Matthew,

    just checked it out, you'll need to use Set-Mailbox anyway - you can't add other addresses with Enable-Mailbox*. You won't need to specify the protocol for PrimarySmtpAddress during Enable-Mailbox, but you WILL need it during Set-Mailbox.

    Cheers,
    Fred

    *Observation made with Exchange 2010, but I doubt this has changed since


    There's no place like 127.0.0.1


    • Edited by FWN Friday, March 18, 2016 8:27 AM
    • Marked as answer by Matthew van Vliet Friday, March 18, 2016 8:52 AM
    Friday, March 18, 2016 8:27 AM
  • Thanks. So will it be like this or am I wrong?

    Import-Csv \\test-test.corp\dfs\ICTData\NieuweMedewerker\Afas02.csv
    Select-Object *,@{Name='SIPone';Expression={'$($_.UserPrincipalName)@test-test.nl'}} |
    Select-Object *,@{Name='SMTPone';Expression={'$($_.UserPrincipalName)@test-test.nl'}} |
    Select-Object *,@{Name='SMTPtwo';Expression={'$($_.UserPrincipalName)@test-test.com'}} |
    Select-Object *,@{Name='SMTPthree';Expression={'$($_.UserPrincipalName)@test-test.eu'}} |
    Select-Object *,@{Name='SMTPfour';Expression={'$($_.UserPrincipalName)@testtest.onmicrosoft.com'}} |
    Export-Csv \\test-test.corp\dfs\ICTData\NieuweMedewerker\Afas01.csv -NoTypeInformation	
    
    Import-Csv \\test-test.corp\dfs\ICTData\NieuweMedewerker\Afas01.csv
    Enable-Mailbox -Identity $_.mailNickname
    Set-Mailbox -PrimarySmtpAddress $_.SMTPone -PrimarySmtpAddress $_.SMTPtwo -PrimarySmtpAddress $_.SMTPthree -PrimarySmtpAddress $_.SMTPfour
    Because the first smtp in Exchange (screenshot in first post) is bold with SMTP capitalized and the others aren't bold and non-capitalized so I think I'm wrong here.

    Also, how do I go about adding the SIP address or will it automatically be generated on using Enable-Mailbox?




    Friday, March 18, 2016 8:32 AM
  • Hi Matthew,

    actually, no, that's not how you go about it. In a cmdlet, you can only add each parameter once. For learning how to use Set-Mailbox, check out its online documentation. Specifically the parameter description of the parameter -EmailAddresses.

    You can safe the output of a cmdlet in a variable:

    # Store output in variable
    $user = Get-ADuser "Norman.Normal"
    
    # Reuse information for whatever you want
    $user | Set-ADuser -HomePhone "12345"

    So there is no need to write it to file, just to reimport it again.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Friday, March 18, 2016 8:44 AM
  • Thanks for the link.

    I was aware of the variable saving but I just want to check after every step if the output is correct, so that's why I export after every step.

    Friday, March 18, 2016 8:52 AM