Yarka
Yarka

Reputation: 13

Attribute value not acceptable in Powershell Automate script

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

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."`

Upvotes: 0

Views: 241

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174690

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

Upvotes: 1

Related Questions