locked
New-ADUser: The name provided is not a properly formed account name RRS feed

  • Question

  • ###########
    #Sample 1
    ###########
    $FirstName = "Bob"
    $LastName = "Bobertson"
    
    $newUserParams = @{
    	Name = ("{0} {1}" -f $FirstName, $LastName) 
    	Path = "OU=Test1,OU=Test2,OU=Test3,OU=Test4,OU=Test5,DC=Everything,DC=local"
    }
    try   {
    	$newUser = New-ADUser $newUserParams 
       }
    catch {
    		"There was an issue creating {0} {1}. {2}" -f $user.LastName, $user.FirstName, $_
       }
    
    ##New-ADUser: Access is denied
    
    ###########
    #Sample 2
    ###########
    
    
    $FirstName = "Bob"
    $LastName = "Bobertson"
    
    $newUserParams = @{
    	Name = ("{0} {1}" -f $FirstName, $LastName) 
    	
    }
    try   {
    	$newUser = New-ADUser $newUserParams -Path "OU=Test1,OU=Test2,OU=Test3,OU=Test4,OU=Test5,DC=Everything,DC=local"
       }
    catch {
    		"There was an issue creating {0} {1}. {2}" -f $user.LastName, $user.FirstName, $_
       }
    
    ##New-ADUser: The name provided is not a properly formed account name
    ###########
    
    ###########

    I am very new to the power shell and I want to understand the mistake I am doing in the above code. 

    In the Sample # 1 - I got "access is denied"

    In the Sample # 2 - I got " The name provided is not a properly formed account name"

    I do have the "manage user privilege and I am able to create the new-aduser with the below code. 

    $First_Name = "Bob"
    $Last_Name = "Bobertson"
    $Sam_Account_Name = "{0}{1}" -f $First_Name.Substring(0,1), $Last_Name
    $Friendly_Name = ("{0} {1}" -f $First_Name, $Last_Name)
    $Job_Title = "Accounting Analyst I"
    $AccountPassword = "P@ssword1" | ConvertTo-SecureString -AsPlainText -Force
    
    $newUsrParams = @{
        Name = $Friendly_Name
        Title = $Job_Title 
        GivenName =$First_Name 
        Surname = $Last_Name 
        DisplayName = $Friendly_Name 
        SamAccountName = $Sam_Account_Name 
        UserPrincipalName = "{0}@mycompany.com" -f $Sam_Account_Name 
        AccountPassword = $AccountPassword 
        Enabled = $true 
        ChangePasswordAtLogon = $true
        ErrorAction = "Stop"
        Path = "OU=Test1,OU=Test2,OU=Test3,OU=Test4,OU=Test5,DC=Everything,DC=local"
    }
    try {
        $newUser = New-ADUser @newUsrParams
    }
    catch {
        "There was an issue creating {0}. {1}" -f $Sam_Account_Name, $_
    }

    Please advise. TIA.




    • Edited by vishsql Wednesday, March 25, 2020 9:29 PM
    Wednesday, March 25, 2020 9:28 PM

Answers

All replies

  • Please post the full error message and not jsut the text.  That tells us what is malformed and where.


    \_(ツ)_/

    Wednesday, March 25, 2020 10:03 PM
  • To splat a param set use "@".

    New-ADUser @newUserParams  

    You didn't do that in your first two examples.


    \_(ツ)_/

    • Marked as answer by vishsql Thursday, March 26, 2020 12:46 AM
    Wednesday, March 25, 2020 10:13 PM
  • thank you for the reply. 

    I would like to know how can I get a full error message. Currently, I am calling the script in the console and that is the only text being returned. 




    • Edited by vishsql Thursday, March 26, 2020 12:42 AM
    Thursday, March 26, 2020 12:42 AM
  • thank you much. it worked perfect.


    • Edited by vishsql Thursday, March 26, 2020 12:47 AM
    Thursday, March 26, 2020 12:47 AM
  • You are not returning the error but are translating it into text. 

    Use "Throw $_" to return the error.  Changing it into text makes it unusable.


    \_(ツ)_/

    Thursday, March 26, 2020 12:47 AM
  • I have "$_" in the catch block but, for some reason it is not going into catch block.

    I tried to run the sample # 1 including additional parameters "telephoneNumber = $user.WorkPhone"

    I was hoping for the catch block to throw error message with error description but, I got the below message. 

    "New-ADUser: A parameter cannot be found that matches parameter name 'telephoneNumber'."

    but, I have that as one of the attribute in the AD. please advise. thank you. 



    • Edited by vishsql Thursday, March 26, 2020 12:56 AM
    Thursday, March 26, 2020 12:56 AM
  • The New-ADUser cmdlet has parameters -Name and -Path, but not -TelephoneNumber. See help doc:

    https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2008-R2-and-2008/ee617253(v=technet.10)

    The parameter -OfficePhone is used to assign values to the telephoneNumber attribute.

    Edit: Also, a value for sAMAccountName is required (the -SamAccountName parameter).


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)


    Thursday, March 26, 2020 1:25 AM
  • Thank you, Richard. 




    • Edited by vishsql Thursday, March 26, 2020 5:25 PM
    Thursday, March 26, 2020 5:25 PM