我尝试在我创建的OU中上载用户,并且我有包含以下字段的.csv文件:名字、姓氏、用户名、密码
下面是正在运行的脚本,当我运行它时得到这个错误:“操作失败,因为为添加/修改提供的UPN值在整个森林范围内不是唯一的.”
导入-模块活动目录
$ADUsers =导入-csv;.\ nameofcsv.csv
foreach ($ADUsers中的$User){
$Username = $User.username
$Password = $User.password
$Firstname = $User.firstname
$Lastname = $User.lastname
$OU = $User.ou
if (Get-ADUser -F {SamAccountName -eq $Username})
{
Write-Warning "A user account with username $Username already exist in Active Directory."
}
else
{
#Account will be created in the OU provided by the $OU variable read from the CSV file
New-ADUser `
-SamAccountName $Username `
-UserPrincipalName "$Username@domainname" `
-Name "$Firstname $Lastname" `
-GivenName $Firstname `
-Surname $Lastname `
-Enabled $True `
-DisplayName "$Lastname, $Firstname" `
-Path $OU `
-AccountPassword (convertto-securestring $Password -AsPlainText -Force)
}
}
发布于 2020-12-05 07:58:47
代码在我看来没问题。实际上我以前也用过它,如果(Get-ADUser -F {SamAccountName -eq $Username})对我来说非常熟悉。
关于您的代码,请参阅
CSV文件?
另外,如果在New-ADuser行的末尾添加-whatif或-debug,会发生什么情况?它应该像您所说那样通过第一个用户,然后在下一行生成某种类型的错误。
https://stackoverflow.com/questions/64153894
复制相似问题