0

I am currently in the process of setting up an AD user creation script with questions.

After answering all the questions here is the error that appears.

New-ADUser: An attribute value was not within the acceptable range At character E: \ SCRIPT \ SCRIPT_CREATE_USER_QUESTION.ps1: 17: 5

  • New-ADUser -SamAccountName $ Username `

The original code is : `Import-Module ActiveDirectory

$Prenom      = Read-Host "Merci de rentrer le prénom de l'utilisateur à créer "
$Nom         = Read-Host "Merci de rentrer le nom de l'utilisateur à créer "
$Password    = Read-Host "Merci de rentrer le mot de passe en respectant la politique actuel "
$Description = Read-Host "Merci de rentrer l'intitulé du poste "


$FirstLetter            = $Prenom.Substring(0,1).ToLower()
$TwoLetter              = "$Prenom.Substring(0,2)"
$FirstLetterName        = "$Nom.Substring(0,1)"
$NomMinuscule           =  $Nom.ToLower() 
$Username               = "$FirstLetter$NomMinuscule"
$Init                   = "$TwoLetter$FirstLetterName.ToUpper()"
$Chemin                 = "OU=LBC-USERS,DC=lbcdom,DC=local"

New-ADUser -SamAccountName $Username `
                -UserPrincipalName "[email protected]" `
                -Name "$Prenom $Nom" `
                -GivenName $Prenom `
                -Surname $Nom `
                -Enabled $True `
                -DisplayName "$Nom, $Prenom" `
                -AccountPassword (convertto-securestring $Password -AsPlainText -Force) `
                -Description $Description `
                -Initials $Init `
                -EmailAddress "[email protected]" `
                -ProfilePath "\\SRV-WINLBC\Profils_itinerants\$Username" `
                -Path $Chemin `
                -ChangePasswordAtLogon $false `
                -PasswordNeverExpires $true `
                -CannotChangePassword $true

          Write-Warning "Bravo! L'utilisateur : $Username est cree."`
4
  • All my attribute seems good i have check Commented Oct 1, 2021 at 14:09
  • The initials attribute must be no longer than 6 characters. Your $Init variable likely has the literal value FirstName.Substring(0,2)FullName.Substring(0,1).ToUpper() because of the way you've constructed your strings Commented Oct 1, 2021 at 14:25
  • I tried without the initials value and i have the same error message ... Commented Oct 1, 2021 at 14:29
  • Yeah, now it probably complains because the username contains invalid characters. Please see the answer I posted below Commented Oct 1, 2021 at 14:31

1 Answer 1

1

I suspect the range violation is because you're passing a string that's way too long for the -Initials argument - the initials attribute must be no longer than 6 characters, but yours is much longer than you think. In addition, the $Username value you construct is not a valid username.

When you do:

$Name = 'Yarka'
$TwoFirstLetters = "$Name.Substring(0,2)"

the resulting literal value of $TwoFirstLetters will be Yarka.Substring(0,2) - PowerShell will expand the $Name variable and ignore the rest.

To avoid this, stop surrounding expressions with ":

$FirstLetter            = $Prenom.Substring(0,1).ToLower()
$TwoLetter              = $Prenom.Substring(0,2)
$FirstLetterName        = $Nom.Substring(0,1)
$NomMinuscule           = $Nom.ToLower() 
$Username               = "$FirstLetter$NomMinuscule"
$Init                   = "$TwoLetter$FirstLetterName".ToUpper()

If you must embed a method call in a string literal, make sure you escape the expression with the subexpression operator $():

$TwoLetter = "$($Prenom.Substring(0,2))" # this will work too
Sign up to request clarification or add additional context in comments.

4 Comments

If i delete all the ", i have some unexpected token ... (i'm sorry i'm a newbie)
@Yarka What if you remove the existing 6 lines (starting from $FirstLetter = ...) and replace them with a copy-paste of the 6 lines in my answer?
You are the best bro thx
@Yarka You're welcome! If my answer solves your problem, please consider marking it "accepted" by clicking the checkmark on the left :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.