New-ADUser From CSV

Tagged: 

This topic contains 6 replies, has 3 voices, and was last updated by Profile photo of Itamar Tziger Itamar Tziger 2 years, 6 months ago.

  • Author
    Posts
  • #15654
    Profile photo of Itamar Tziger
    Itamar Tziger
    Participant

    this is my code

    Import-Csv C:\Users\Administrator\Desktop\ComputersUsers.csv |foreach {New-ADUser -GivenName $_.GivenName -Surname $_.Surname -DisplayName "$_.DisplayName" -SamAccountName $_.SamAccountName -UserPrincipalName $_.UserPrincipalName -AccountPassword(ConvertTo-SecureString -AsPlainText "1234" -Force) -Enabled $true -ChangePasswordAtLogon $false -Name "$_.DisplayName" -Path "$_.OU" }

    this is my CSV
    GivenName,Surname,DisplayName,SamAccountName,UserPrincipalName,accountpassword,OU
    Namal,Computers1,Namal Computers 1,Computers1,Computers1@students.local,1234,"""OU=ComputersRoom,OU=Namal,OU=Users,OU=StudentsLocal,DC=students,DC=local"""
    Namal,Computers2,Namal Computers 2,Computers2,Computers2@students.local,1234,"""OU=ComputersRoom,OU=Namal,OU=Users,OU=StudentsLocal,DC=students,DC=local"""
    Namal,Computers3,Namal Computers 3,Computers3,Computers3@students.local,1234,"""OU=ComputersRoom,OU=Namal,OU=Users,OU=StudentsLocal,DC=students,DC=local"""
    Namal,Computers4,Namal Computers 4,Computers4,Computers4@students.local,1234,"""OU=ComputersRoom,OU=Namal,OU=Users,OU=StudentsLocal,DC=students,DC=local"""
    Namal,Computers5,Namal Computers 5,Computers5,Computers5@students.local,1234,"""OU=ComputersRoom,OU=Namal,OU=Users,OU=StudentsLocal,DC=students,DC=local"""
    Namal,Computers6,Namal Computers 6,Computers6,Computers6@students.local,1234,"""OU=ComputersRoom,OU=Namal,OU=Users,OU=StudentsLocal,DC=students,DC=local"""
    Namal,Computers7,Namal Computers 7,Computers7,Computers7@students.local,1234,"""OU=ComputersRoom,OU=Namal,OU=Users,OU=StudentsLocal,DC=students,DC=local"""
    Namal,Computers8,Namal Computers 8,Computers8,Computers8@students.local,1234,"""OU=ComputersRoom,OU=Namal,OU=Users,OU=StudentsLocal,DC=students,DC=local"""
    Namal,Computers9,Namal Computers 9,Computers9,Computers9@students.local,1234,"""OU=ComputersRoom,OU=Namal,OU=Users,OU=StudentsLocal,DC=students,DC=local"""
    Namal,Computers10,Namal Computers 10,Computers10,Computers10@students.local,1234,"""OU=ComputersRoom,OU=Namal,OU=Users,OU=StudentsLocal,DC=students,DC=local"""
    Namal,Computers11,Namal Computers 11,Computers11,Computers11@students.local,1234,"""OU=ComputersRoom,OU=Namal,OU=Users,OU=StudentsLocal,DC=students,DC=local"""
    Namal,Computers12,Namal Computers 12,Computers12,Computers12@students.local,1234,"""OU=ComputersRoom,OU=Namal,OU=Users,OU=StudentsLocal,DC=students,DC=local"""
    Namal,Computers13,Namal Computers 13,Computers13,Computers13@students.local,1234,"""OU=ComputersRoom,OU=Namal,OU=Users,OU=StudentsLocal,DC=students,DC=local"""
    Namal,Computers14,Namal Computers 14,Computers14,Computers14@students.local,1234,"""OU=ComputersRoom,OU=Namal,OU=Users,OU=StudentsLocal,DC=students,DC=local"""
    Namal,Computers15,Namal Computers 15,Computers15,Computers15@students.local,1234,"""OU=ComputersRoom,OU=Namal,OU=Users,OU=StudentsLocal,DC=students,DC=local"""
    Namal,Computers16,Namal Computers 16,Computers16,Computers16@students.local,1234,"""OU=ComputersRoom,OU=Namal,OU=Users,OU=StudentsLocal,DC=students,DC=local"""

    Why is not working correctly?

    ERROR:

    New-ADUser : The object name has bad syntax
    At C:\PowershellScript\New-Aduser from csc.ps1:2 char:72
    + Import-Csv C:\Users\Administrator\Desktop\ComputersUsers.csv |foreach {New-ADUse ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (CN=@{GivenName\...s,DC=local"}.OU:String) [New-ADUser], ADException
    + FullyQualifiedErrorId : The object name has bad syntax,Microsoft.ActiveDirectory.Management.Commands.NewADUser

    Thank you for your help

    Itamar.

  • #15655
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    You've got quotation marks embedded in your CSV file for the OU field. I haven't tested your code, but that seems to be the most likely cause of a problem, at first glance. Try this, to see if it fixes the problem:

    -Path $_.OU.Trim('"')
    
  • #15656
    Profile photo of Itamar Tziger
    Itamar Tziger
    Participant

    First TNX.
    I test my code and delete PATH from my script doesn't help.
    also with ur code doesn't help

    any suggestion ?

  • #15659
    Profile photo of Daniel Krebs
    Daniel Krebs
    Participant

    Please attach your script and the CSV. I would like to run this against my test AD today evening.

    Thanks
    Daniel

  • #15661
    Profile photo of Itamar Tziger
    Itamar Tziger
    Participant

    I attach the file.
    tnx.

  • #15790
    Profile photo of Itamar Tziger
    Itamar Tziger
    Participant

    Thank i will try it tomorrow

    Itamar

  • #15789
    Profile photo of Daniel Krebs
    Daniel Krebs
    Participant

    Hello Itamar,

    Apologies for not getting back to you earlier on this. Unfortunately something else came up on Wednesday evening which prevented me to look into this and Thursday / Friday were already planned out.

    The good news I can explain why it does not work for you and provide solutions.

    1. The cue is actually in the error message. The distinguished name for the user account is not correct (CN=@{GivenName\…s,DC=local”}.OU:String)

    Why? You've used double quotes to expand "$_.DisplayName" and "$_.OU" as strings but did not use the correct format for these expressions.

    What happens is that PowerShell will expand the $_ object into the string and append .DisplayName and .OU. You can actually see this easily if you leverage the -WhatIf switch for New-ADUser.

    Example:
    Import-Csv C:\Users\Administrator\Desktop\ComputersUsers.csv |foreach {New-ADUser .... -WhatIf }

    Output:
    What if: Performing the operation "New" on target "CN=@{GivenName\=Namal\; Surname\=Computers1\; DisplayName\=Namal Computers 1\; SamAccountName\=Computers1\; UserPrincipalName\=Comp
    uters1@students.local\; accountpassword\=1234\; OU\=\"OU\=ComputersRoom\,OU\=Namal\,OU\=Users\,OU\=StudentsLocal\,DC\=students\,DC\=local\"}.DisplayName,@{GivenName=Namal; Surname=Co
    mputers1; DisplayName=Namal Computers 1; SamAccountName=Computers1; UserPrincipalName=Computers1@students.local; accountpassword=1234; OU="OU=ComputersRoom,OU=Namal,OU=Users,OU=Stude
    ntsLocal,DC=students,DC=local"}.OU".
    ....

    Solution 1:
    $_.DisplayName and $_.OU need to be handled as expression in the quoted string.

    -Name "$( $_.DisplayName )" -Path "$( $_.OU )"

    Solution 2:
    Just omit the double quotes and assign directly which I would prefer.

    -Name $_.DisplayName -Path $_.OU

    2. The other problem is your OU column in the CSV input file. You've specified triple quotes to escape the commas in the distinguished name of the OU path.

    """OU=ComputersRoom,OU=Namal,OU=Users,OU=StudentsLocal,DC=students,DC=local"""

    Solution 1:
    Clean up the input file and only use one double quote at both ends of the OU path.
    "OU=ComputersRoom,OU=Namal,OU=Users,OU=StudentsLocal,DC=students,DC=local"

    Solution 2:
    Remove the double quotes you've imported while you assigned it to the -Path parameter.

    Example 1:
    -Path "$( $_.OU.Trim('"') )"

    Example 2:
    -Path $_.OU.Trim('"')

    I hope my answer helps you to get back on track.

    Best,
    Daniel

You must be logged in to reply to this topic.