我在创建用户脚本中收到以下错误。
ConvertTo-SecureString:无法将参数绑定到参数“String”,因为它是空字符串。 在 C:\AD_Scripts\psscripts\user_create.ps1:59 字符:54 + -AccountPassword (convertto-securestring "$Password" -AsPlainText -F ... + ~~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [ConvertTo-SecureString],ParameterBindingValidationException + FullQualifiedErrorId: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 "[email protected]" `
-Name "$Firstname $Lastname" `
-GivenName $Firstname `
-Department "$Department" `
-Company "$Company" `
-EmailAddress "[email protected]" `
-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","测试","学生1","20001225","2016","1480" "333333","测试","学生3","2001225","2025","1479"
我使用 Bdate 作为用户密码
所以这里有两个问题,首先你从来没有声明
$Password
这解释了你得到的错误,因为你将 null
值传递给convertto-securestring,你还想删除变量周围的引号,他们不会'没有破坏任何东西,但它们不符合惯例。所以改变
-AccountPassword (convertto-securestring "$Password" -AsPlainText -Force)
到
-AccountPassword (convertto-securestring $User.BDATE -AsPlainText -Force)
您还应该看看您的 If 语句,旨在防止您的脚本尝试创建已存在的用户,您的 LDAP 过滤器
{$Username=$user.$SamAccountName}
将始终返回 false,因为它不是有效的格式,您真的不应该比较运算符两侧的变量和 $user.$SamAccountName 在您的脚本中不存在。这并不是什么真正重要的事情,因为如果用户已经存在,new-aduser 无论如何都会自行出错。