问题 无法将String转换为安全字符串以在New-ADUser中使用


我正在使用Primal Forms Community Edition来创建一个GUI,它将为我们的秘书简化新的学生创建过程。在较低级别的学校,学生使用他们的生日作为他们的密码,因此他们很容易记住。

我有一个标记为“生日”字段的文本输入框。我想要做的是将该字段用于New-ADUser中的-AccountPassword。但是,无论我尝试什么,我总是在尝试使用我的脚本创建新用户时出现此错误。

New-ADUser : Cannot bind parameter 'AccountPassword'. Cannot convert the "System.Security.SecureString" value of type 
"System.String" to type "System.Security.SecureString".
At C:\Users\pomeroyt\Google Drive\Work\Scripts\Powershell\student_creation_gui.ps1:377 char:12
+ New-ADUser @User
+            ~~~~~
    + CategoryInfo          : InvalidArgument: (:) [New-ADUser], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.ActiveDirectory.Management.Commands.NewADUser

我使用的代码看起来像这样。

$password = $dob.text | ConvertTo-SecureString -AsPlainText -Force
$user = @{
    Description = "Student"
    UserPrincipalName = "$username@domain.local"
    Name = "$lname.text, $fname.text"
    SamAccountName = "$username"
    Surname = "$lname.text" 
    GivenName = "$fname.text" 
    EmailAddress = "$email" 
    HomeDrive = H: 
    HomeDirectory = "\\$server\Students\$yog\$username" 
    ScriptPath = "$script" 
    ChangePasswordAtLogon = 0 
    CannotChangePassword = 1 
    PasswordNeverExpires = 1 
    AccountPassword = "$password"
    Enabled = 1
    Path = "OU=$yog,OU=$group,OU=STUDENTS,DC=domain,DC=local"
    }
New-ADUser @User

我真的很茫然,因为我所看到的一切都说我正在做的事应该奏效

编辑 -

下面的解决方案确实解决了密码问题。但是,我没有意识到我实际上看到了我的代码的其他问题。

我打开-verbose看看发生了什么,发现Name字段输出不正确。为Name =输入“$ lname,$ fname”时由于某种原因导致$ lname的完整输出。我创建了一个名为$ name的新字符串,并将其设置为= $ lname.text +“,”+ $ fname.text。

现在Name = $ name,命令按预期触发。


11325
2017-10-04 18:56


起源

我在你的Excel问题上得到了答案,但是在我发布之前你删除了它。 - Lance Roberts


答案:


更改

AccountPassword = "$password"

AccountPassword = $password

如果您在变量周围有引号,则将其视为常规字符串而不是安全字符串。证明:

$plainText = "Plain text"
$secureString = ConvertTo-SecureString $plainText -AsPlainText -Force
$quotedSecureString = "$secureString"
$plainText.GetType()
$secureString.GetType()
$quotedSecureString.GetType()

结果是

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object
True     False    SecureString                             System.Object
True     True     String                                   System.Object

13
2017-10-04 19:43



当我这样做但是我最终得到了这个错误: New-ADUser : A value for the attribute was not in the acceptable range of values At C:\Users\****\Google Drive\Work\Scripts\Powershell\student_creation_gui.ps1:377 char:1 + New-ADUser @User + ~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (CN=System.Windo...afford,DC=local:String) [New-ADUser], ADException + FullyQualifiedErrorId : ActiveDirectoryServer:8322,Microsoft.ActiveDirectory.Management.Commands.NewADUser - ParadoxCTRL