locked
How to take value from "PrimarySmtpAddress" and paste it into "WindowsEmailAddress" in Powershell? RRS feed

  • Question

  • I have mail-enabled contacts that are missing WindowsEmailAddress. I need to propogate PrimarySmtpAddress into WindowsEmailAddress field. I have 3 commands in mind and I'm trying to figure out how to pipe them right.
    Something like:

    Get-MailContact | FL PrimarySmtpAddress | Set-MailContact -WindowsEmailAddress

    But it errors out with:
    Set-MailContact : Missing an argument for parameter 'WindowsEmailAddress'. Specify a parameter of type 'Microsoft.Exchange.Data.SmtpAddress' and try again.
    At line:1 char:78
    + Get-MailContact | FL PrimarySmtpAddress | set-mailcontact -WindowsEmailAddress <<<<


    May be I need to put together some script and save it as .PS1 file?
    Any ideas?

    Thanks
    Tuesday, August 12, 2008 4:54 PM

Answers

  • No, I didn’t try that since my test environment is being rebuilt. Here is the script you can try with if you are still looking for it...(again, I didn’t try this but theoretical it should work)

     

    ========================================

    $Contacts = Get-MailContact “MailContactName”

    ForEach ($Contact in $Contacts)

    {

    $WEA = $Contact.PrimarySmtpAddress

    $WEA = $WEA.ToString()

    Set-Contact $Contact - WindowsEmailAddress $WEA

    $WEA = $Null

    }

    ========================================

     

    You can run on all Contact by this…

    ========================================

    $Contacts = Get-MailContact

    ForEach ($Contact in $Contacts)

    {

    $WEA = $Contact.PrimarySmtpAddress

    $WEA = $WEA.ToString()

    Set-Contact $Contact - WindowsEmailAddress $WEA

    $WEA = $Null

    }

    ========================================

    Thursday, August 14, 2008 9:04 AM

All replies

  • For one Mail Contact...

    Get-MailContact MailContactName” | Set-MailContact -WindowsEmailAddress $_.PrimarySmtpAddress

     

    Verify it...

    Get-MailContact MailContactName” | FL Name, PrimarySmtpAddress, WindowsEmailAddress

     

    For all MailContact in your environment…

    Get-MailContact | Set-MailContact -WindowsEmailAddress $_.PrimarySmtpAddress

     

    Tuesday, August 12, 2008 5:19 PM
  • Thanks Amit. Did it actually work for you? Because I'm getting some error:

    [PS] C:\2>Get-MailContact "Tim Sage" | FL DisplayName,PrimarySmtpAddress,WindowsEmailAddress


    DisplayName         : Tim Sage
    PrimarySmtpAddress  : aaa@aaa.aaa
    WindowsEmailAddress :



    [PS] C:\2>Get-MailContact "Tim Sage" | Set-MailContact -WindowsEmailAddress $_.PrimarySmtpAddress

    Set-MailContact : Cannot bind parameter 'WindowsEmailAddress' to the target. Exception setting "WindowsEmailAddress": "Object reference not set to an instance of an object."
    At line:1 char:66
    + Get-MailContact "Tim Sage" | Set-MailContact -WindowsEmailAddress  <<<< $_.PrimarySmtpAddress
    Tuesday, August 12, 2008 6:42 PM
  • I tried this as well and it did not work for me.  You may actually not be able to accomplish this with a one-liner.   If you set the value of Get-MailContact to a variable and then look at $VarName.PrimarySMTPAddress, you will see that the information is not actually presented as an SMTP.  I think you can apply a method to it to set the output value to a string, but I could not figure it out.  Of course, it is late there.  :-(

    Wednesday, August 13, 2008 6:59 AM
  • No, I didn’t try that since my test environment is being rebuilt. Here is the script you can try with if you are still looking for it...(again, I didn’t try this but theoretical it should work)

     

    ========================================

    $Contacts = Get-MailContact “MailContactName”

    ForEach ($Contact in $Contacts)

    {

    $WEA = $Contact.PrimarySmtpAddress

    $WEA = $WEA.ToString()

    Set-Contact $Contact - WindowsEmailAddress $WEA

    $WEA = $Null

    }

    ========================================

     

    You can run on all Contact by this…

    ========================================

    $Contacts = Get-MailContact

    ForEach ($Contact in $Contacts)

    {

    $WEA = $Contact.PrimarySmtpAddress

    $WEA = $WEA.ToString()

    Set-Contact $Contact - WindowsEmailAddress $WEA

    $WEA = $Null

    }

    ========================================

    Thursday, August 14, 2008 9:04 AM
  • Thanks Amit. Did it actually work for you? Because I'm getting some error:

    [PS] C:\2> Get-MailContact "Tim Sage" | FL DisplayName,PrimarySmtpAddress,WindowsEmailAddress


    DisplayName         : Tim Sage
    PrimarySmtpAddress  : aaa@aaa.aaa
    WindowsEmailAddress :



    [PS] C:\2> Get-MailContact "Tim Sage" | Set-MailContact -WindowsEmailAddress $_.PrimarySmtpAddress

    Set-MailContact : Cannot bind parameter 'WindowsEmailAddress' to the target. Exception setting "WindowsEmailAddress": "Object reference not set to an instance of an object."
    At line:1 char:66
    + Get-MailContact "Tim Sage" | Set-MailContact -WindowsEmailAddress  <<<< $_.PrimarySmtpAddress
    This is exactly 100% what is happening to me.  Did you find a fix for this?  The contacts show as having the correct (and only) primary smtp, but I get the "object reference not set to an instance of an object." when i actually run the code to set-mailcontact ....

    Any help would be appreciated.
    Thursday, April 9, 2009 1:11 AM
  • What about using the loging account and not your display name ? "Tim Sage"
    Wednesday, June 2, 2010 6:40 AM
  • I did this command and works form me

     

    get-mailbox username |fl primarysmtpaddress

     

    Greetings

    Wednesday, March 9, 2011 6:18 AM
  • Get-MailContact “MailContactName” | Set-MailContact -WindowsEmailAddress $_.PrimarySmtpAddress
     
    Quote Evan Dodds MSFT

    Will not work because in this syntax, the pipeline object variable ($_) is not available. Since you’re directly pipelining from Get | Set here, we bind the output object to instance parameter and stream the data into the Set. There is no explicit iteration, and no script-block to host the $_ variable.
     
    I don’t know that this syntax below does what you want, but at the very least it’ll activate the pipeline object variable by injecting a foreach-object explicit iteration into the operation (and also doing a per-iteration explicit binding of the pipeline object to identity parameter since we’re losing the implicit pipeline binding we had in the earlier syntax):
     
    Get-MailContact “MailContactName” | % { Set-MailContact $_-WindowsEmailAddress $_.PrimarySmtpAddress }


    James Chong MCITP | EA | EMA; MCSE | M+, S+ Security+, Project+, ITIL msexchangetips.blogspot.com
    Wednesday, March 9, 2011 7:05 PM
  • Hi all

    Came across this problem as well after a year of managing Exchange, yikes!

    See KB for info and fix http://support.microsoft.com/kb/936197

     

    Before finding the KB I used the following:

    Firstly check the email address policy format for the primary smtp address, example: %g.%s@domain

    Then run script to search for the users whose addresses do not match (causing them to be dropped out of the GAL) and pipe them to have the format of their windows email addresses to be set to the smae format as the primary smtp address.

     

    get-mailbox -resultsize unlimited | Where-Object {$_.PrimarySMTPAddress -ne $_.WindowsEmailAddress} | Set-Mailbox -WindowsEmailAddress %g.%s@domain 

     

    The one in the KB admittedly appears to be a more solid solution!

    Thanks

    • Proposed as answer by Karl Ernstzen Thursday, September 20, 2012 2:11 PM
    Friday, January 13, 2012 8:12 AM