Creating New contacts through script

Welcome Forums General PowerShell Q&A Creating New contacts through script

This topic contains 5 replies, has 3 voices, and was last updated by

 
Participant
9 months, 2 weeks ago.

  • Author
    Posts
  • #92396

    Participant
    Points: 0
    Rank: Member

    Hello,

    I'm failing with "New-ADObject : The object name has bad syntax" in the following script:

     foreach ($i in $contacts) {
    $nicnkame=$i.replace("@","_")
    
    New-ADObject -Name $i -Type Contact -Path "ou=Us-contacts,ou=ou=US,dc=corp,dc=pri" -OtherAttributes @{'mail'=@($i)} 
    
    } 

    In $contacts variable I have few strings:

    $contacts=@(
    "mari@zdv.com"
    "steph@zdv.com"
    "sofio@zdv.com"
    "carol@zdv.com"
    )

    I've tried already to change name parameter with $($i) or "$($I)". Also can someone please explain why sometimmes is neccessary to use $($i) instead of $i.

    Kind regards,
    Dorian

  • #92401
    Jon

    Participant
    Points: 25
    Rank: Member

    What is the purpose of the nickname variable? I don't see you using it anywhere else in the code.

    Give this a read about sub expressions:

    https://www.pdq.com/blog/using-subexpressions-within-strings/

  • #92404

    Participant
    Points: 1
    Rank: Member

    "The object name has bad syntax"
    Looking at your script, you´re not using the variable you created on the second line (-Name $i should be -Name $nicnkame). As for the -Otherattributes hashtable, the values should probably be strings, so "$i" or "$($i)", in this case either should work (if $i already is a string "" is not needed). In a string $() tells powershell that what´s inside is a variable or function that needs to be resolved. If $i had multiple values, and you just wanted the value for name, in a string "$i.name" would not work. The .name would be red as a string, you would need to use "$($i.name)".

  • #92471

    Participant
    Points: 0
    Rank: Member

    Hello guys,

    Thank you for the answers.

    Actually the $nickname is for mailnickname attribute which I wanted to specify later.

    But even if I run this

     New-ADObject -Name "$($i)" -Type Contact -Path "ou=Us-contacts,ou=ou=US,dc=corp,dc=pri" 

    It fails with the same error.

    Best regards

  • #92492
    Jon

    Participant
    Points: 25
    Rank: Member

    What version of Exchange are you running? If you are running 2016 or Exchange online, have you tried the New-MailContact cmdlet?

    New-MailContact

    Another thing to try. Put your contacts in a text file and run the following code:

    Get-Content C:\contacts.txt | foreach {New-Adobject -name $_ -Type Contact -Path YOUROU}
  • #92773

    Participant
    Points: 0
    Rank: Member

    The problem was solved after I've copied the distinguishedname instead of typing it. Not sure if it is case sensitive but I couldn't find a typo.

    Best regards

The topic ‘Creating New contacts through script’ is closed to new replies.