【发布时间】:2017-03-31 02:55:59
【问题描述】:
我在创建用户脚本中收到以下错误。
ConvertTo-SecureString:无法将参数绑定到参数“String”,因为它是一个空字符串。 在 C:\AD_Scripts\psscripts\user_create.ps1:59 char:54 + -AccountPassword (convertto-securestring "$Password" -AsPlainText -F ... + ~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [ConvertTo-SecureString], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.ConvertToSecureStringCommand
# Import active directory module for running AD cmdlets
Import-Module activedirectory
#Store the data from ADUsers.csv in the $ADUsers variable
$ADUsers = Import-csv userimport.csv
#Store report in log file in the $log variable
$log = "log.txt"
#Loop through each row containing user details in the CSV file
foreach ($User in $ADUsers)
{
#Read user data from each field in each row and assign the data to a variable as below
$Username = $User.ID
$Password = $User.BDATE
$Firstname = $User.FNAME
$Lastname = $User.LNAME
$Department = $User.GRD
$Company = $User.SCHID #This field refers to the OU the user account is to be moved to
# Choose OU
Switch ($Company)
{
"1480" {$OU = 'OU=students,OU=users,ou=hs,dc=clasd,dc=net'}
"1479" {$OU = 'OU=students,OU=users,ou=elem,dc=clasd,dc=net'}
"1480" {$Folder = '\\hs-ss\students\hs'}
"1479" {$Folder = '\\hs-ss\students\elem'}
}
#Check to see if the user already exists in AD
if (Get-ADUser -LDAPFilter {$Username=$user.$SamAccountName})
{
#If user does exist, give a warning
Write-Warning "A user account with username $Username already exist in Active Directory."
}
else
{
#User does not exist then proceed to create the new user account
"Processing started (on " + $date + "): " | Out-File $log -append
"--------------------------------------------" | Out-File $log -append
#Account will be created in the OU provided by the $OU variable read from the CSV file
New-ADUser `
-SamAccountName $Username `
-UserPrincipalName "$Username@clasd.net" `
-Name "$Firstname $Lastname" `
-GivenName $Firstname `
-Department "$Department" `
-Company "$Company" `
-EmailAddress "$Username@clasd.net" `
-Surname $Lastname `
-Enabled $True `
-Scriptpath "login.vbs" `
-DisplayName "$Firstname $Lastname" `
-Path $OU `
-Homedrive "Z" `
-homedirectory "$Folder\$username" `
-AccountPassword (convertto-securestring "$Password" -AsPlainText -Force) `
-ChangePasswordAtLogon $true
}
}
在更改此行之前,我从未收到错误
if (Get-ADUser -F {SamAccountName -eq $Username})
到
if (Get-ADUser -LDAPFilter {$Username=$user.$SamAccountName})
我正在导入的 cvs 文件如下所示:
"ID","FNAME","LNAME","BDATE","GRD","SCHID" "111111","测试","student1","20001225","2016","1480" "333333","test","Student3","2001225","2025","1479"
我使用 Bdate 作为用户密码
【问题讨论】:
-
建议调试。将 $Username=$user.$SamAccountName 分配移到 if 上方,您可以验证 ISE 中的值。此外,对 New-ADUser 的参数使用 splatting,然后您可以在 ISE 中设置断点并检查 $Password 和/或在其为空白时放入带有断点的 if。然后,您还可以将 convertto-securestring 调用放在参数的争吵之外,并分配给另一个变量,如 $securePassword 等。
-
因为我是 Powershell 的新手,所以我得看看你在说什么:-)。你第二个说要使用 splatting 的人
标签: powershell active-directory