Passing a variable RRS feed

  • Question

  • I am trying to write a script that would first get an attribute from AD based on an email address from a list and populate an attribute in another forest with that same value retrieved from the first domain.


    #Get email addresses from the list
    $eadd = GetContenct "C:\t6\names.txt"

    #Get the mailNickName from the user's attribute using the email address to find the user
    $mnn = Get-ADUser -Filter * -Properties Name,mailNickname,mail -SearchBase "OU=Users,OU=Wherever,OU=mycompany,dc=us,dc=domain1,dc=com" -Server "us_servername.domain1.com" | where {$_.mail -eq $eadd} | Select mailNickname

    #Enter the mailNickName attribute value from domain 1 with the same email address into domain 2 mailNickName attribute
    Set-ADUser -Instance $eadd  -Server "otherserver.domain2.com" -Replace @{mailNickName="variable retrived from above($mnn)"}


    Need to get the proper format for the variable $mnn into the set command so it uses the value I get from the Get command properly.

    I am still learning Powershell so maybe I am over engineering this.  Just not sure how to pass the mailNickname value I got from the first domain to the set-aduser command to set the value in the second domain.

    Thanks in advance for you help.


    • Moved by Bill_Stewart Friday, November 20, 2015 7:24 PM This is not "scripts on demand"
    Tuesday, August 18, 2015 6:38 PM

All replies

  • Hi Dave,

    Lets break it in parts. Get the individual components working, then you can have it all together.

    $eadd  is an array of strings as you must be passing multiple usernames@mail.com . Hence if we follow later your script has some issues.

    If $eadd = SingleUser@mail.com  your script should work without issue(provided you fix the syntax errors) Please check this first.

    You can call just the value using @{mailNickName= "$($mnn.mailNickname)"}

    Problem or loophole in your code is that $mnn or $eadd can result in array of names, and your Set- where is not suppose to accept array.

    You would need forloop to select one item at a time

    foreach ($ead in $eadd) {
    "Do work on single $ead"



    Please“Vote As Helpful” if you find my contribution useful or “MarkAs Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.

    • Edited by Satyajit321 Thursday, August 20, 2015 11:06 AM
    Thursday, August 20, 2015 11:04 AM