属性值在Powershell自动化脚本中不被接受



我目前正在设置一个AD用户创建脚本的过程中。

回答完所有问题后,出现的错误如下:

New-ADUser:属性值不在可接受范围内字符E: SCRIPT SCRIPT_CREATE_USER_QUESTION。Ps1: 17:5

  • New-ADUser -SamAccountName $ Username '

原来的代码是:' 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 "$Username@lbcdom.local" `
-Name "$Prenom $Nom" `
-GivenName $Prenom `
-Surname $Nom `
-Enabled $True `
-DisplayName "$Nom, $Prenom" `
-AccountPassword (convertto-securestring $Password -AsPlainText -Force) `
-Description $Description `
-Initials $Init `
-EmailAddress "$NomMinuscule@leboncandidat.fr" `
-ProfilePath "\SRV-WINLBCProfils_itinerants$Username" `
-Path $Chemin `
-ChangePasswordAtLogon $false `
-PasswordNeverExpires $true `
-CannotChangePassword $true
Write-Warning "Bravo! L'utilisateur : $Username est cree."`

我怀疑范围冲突是因为您传递的字符串对于-Initials参数来说太长了-initials属性必须不超过6个字符,但您的比您想象的要长得多。另外,您创建的$Username值不是一个有效的用户名。

当你这样做的时候:

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

$TwoFirstLetters的结果字面值将是Yarka.Substring(0,2)- PowerShell将展开$Name变量并忽略其余部分。

要避免这种情况,请停止在表达式周围使用":

$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()

如果必须在字符串文字中嵌入方法调用,请确保使用子表达式操作符$():

转义表达式。
$TwoLetter = "$($Prenom.Substring(0,2))" # this will work too

最新更新