Swap primarysmtpaddress with another existing emailaddress

This topic contains 9 replies, has 5 voices, and was last updated by Profile photo of Kevan Frazier Kevan Frazier 11 months, 3 weeks ago.

  • Author
  • #31547
    Profile photo of Kevan Frazier
    Kevan Frazier

    I need to make an existing smtp address, in the list of Emailaddresses, the primarysmtpaddress. I need to do this in bulk using a CSV file. I have tried for days to come up with a one liner. The lastest attempt was to make a 2 column CSV with the first column "identity" and a second column "emailaddresses" which I filtered to the address that I want to make the primary smtp address by using select-object. I changed the header on the second column to the valid property "emailaddresses". This was my lastest attempt:
    >Import-Csv c:\test.csv | get-mailbox | foreach {set-mailbox -PrimarySmtpAddress -eq $_.emailaddresses}
    I can run Import-Csv c:\test.csv | get-mailbox which gets the mailboxes, but the set-mailbox doesn't like the input of the second column of the CSV file (emailaddresses).This is the error:
    Cannot process argument transformation on parameter 'PrimarySmtpAddress'. Cannot convert value "-eq" to type "Microsoft
    .Exchange.Data.SmtpAddress". Error: ""-eq" is not a valid SMTP address"
    Thank you for any solutions to this task, which is so easy using the GUI, but impractical for 180 room mailboxes.

  • #31548
    Profile photo of tommymaynard

    When providing a value for a parameter you're not going to need/want/be able to include a comparison operator. This "-PrimarySmtpAddress -eq $_.emailaddresses" is wrong, and should be written as: -PrimarySmtpAddress $_.emailaddresses

    Again, I've yet to see a comparison operator included when assigning a value, in this case $_.emailaddresses, to a parameter, in this case -PrimarySmtpAddress.

  • #31550
    Profile photo of Bob McCoy
    Bob McCoy

    And why does it have to be a one-liner?

  • #31572
    Profile photo of Kevan Frazier
    Kevan Frazier

    Bob and Tommy, thanks so much for replying so quickly.

    I wasn't implying it had to be a one-liner, but it seemed it might be possible to do this with one, and more importantly, I'm very new to PS scripting and trying to learn it.
    Thanks, Kevan

    Thanks for the info. Makes perfect sense and something I should have known with my limited knowledge of PS. However, removing the "-eq" and running the same command:
    [PS] C:\>Import-Csv c:\test.csv | get-mailbox | foreach {set-mailbox -PrimarySmtpAddress $_.emailaddresses}
    I get this error:
    Pipeline not executed because a pipeline is already executing. Pipelines cannot be executed concurrently.

    If I omit "get-mailbox" and run it like this:
    [PS] C:\>Import-Csv c:\test.csv | foreach {set-mailbox -PrimarySmtpAddress $_.emailaddresses}
    I get this error:
    Cannot process argument transformation on parameter 'PrimarySmtpAddress'. Cannot convert null to type "Microsoft.Exchange.Data.SmtpAddress".

    Seems you would need to "get-mailbox" before passing on that data to "set-mailbox"

    Any further assistance would be greatly appreciated

  • #31577
    Profile photo of Curtis Smith
    Curtis Smith

    You can skip using get-mailbox as long as you specify your Identity parameter for Set-MailBox. It is required.

    The Identity parameter specifies the mailbox that you want to modify. You can use any value that uniquely
    identifies the mailbox.

    For example:
    * Name
    * Display name
    * Alias
    * Distinguished name (DN)
    * Canonical DN
    * \
    * Email address
    * GUID
    * LegacyExchangeDN
    * SamAccountName
    * User ID or user principal name (UPN)

    Required? true
    Position? 1
    Default value
    Accept pipeline input? True
    Accept wildcard characters? false

  • #31580
    Profile photo of Kevan Frazier
    Kevan Frazier

    Curtis, thanks for the additional tips. I know the import is correct on the left of the command:
    Import-Csv c:\test.csv
    and I'm pretty sure the loop execution on the right is correct:
    foreach {set-mailbox -PrimarySmtpAddress $_.emailaddresses}

    it's how to pass the identity of each mailbox through the pipeline to:
    foreach {set-mailbox -PrimarySmtpAddress $_.emailaddresses}
    I've tried defining a variable $mailboxes = import-csv c:\test.csv
    and then tried:
    foreach ($mailbox in $mailboxes) {set-mailbox -PrimarySmtpAddress $_.emailaddresses}
    that bombed as well. I thought PS might take the first column of the CSV (identity) to define the mailbox, because it is a positional parameter

  • #31581
    Profile photo of Curtis Smith
    Curtis Smith

    If you used get-help to see the help on set-mailbox, you will see that -identity is a parameter of set-mailbox. Now the question is, does your input file contain the Identity of your mailbox. Does you CSV have column headers? It appears that it does because you are using $_.emailaddresses in your loop. You may have another column that is your Identity like UserPrincipalName. If so you would use this value from your input file with the -identity paramemter of the set-mailbox cmdlet.

    For Example:

    Import-Csv c:\test.csv | foreach-object {set-mailbox -Identity $_.UserPrincipalName -PrimarySmtpAddress $_.emailaddresses}
  • #31582
    Profile photo of Kevan Frazier
    Kevan Frazier

    I tried using your command and it seems simple enough, but still errors out:

    Cannot process argument transformation on parameter 'PrimarySmtpAddress'. Cannot convert null to type "Microsoft.Exchan
    + CategoryInfo : InvalidData: (:) [Set-Mailbox], ParameterBindin...mationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Set-Mailbox

  • #31584
    Profile photo of Max Kozlov
    Max Kozlov


    when you use import-csv | set-mailbox

    if You get PrimarySmtpAddress as null, your column name is not "emailaddresses" or there is another problem with you csv. Seems it imported incorrectly.

    Could you post here first 3 lines of test.csv ?

    if you try to use Import-csv | get-mailbox | set-mailbox, then on set-mailbox stage you have in $_ result of get-mailbox but not your CSV line. you CAN NOT set email addresses from csv this way

  • #31592
    Profile photo of Kevan Frazier
    Kevan Frazier

    Many thanks to you all for your input to my issue. The solution was to replace the second column in the CSV file which was 'emailaddresses' to 'primarysmtpaddress'.
    The command that works is:
    Import-Csv ase.csv | foreach {Set-Mailbox -Identity $_.alias -PrimarySmtpAddress $_.primarysmtpaddress}

    This is the first time I used this forum for help from an expert community. I am so pleased and impressed with all of the prompt replies which all helped lead me to the solution.

    Best regards,
    Kevan Frazier

You must be logged in to reply to this topic.