AD中的批量用户

时间:2014-12-06 18:00:11

标签: powershell scripting active-directory windows-server-2008 server

我正在尝试使用下面的脚本使用PowerShell创建批量用户,但我收到的错误如下所述。请帮忙!

剧本:

Import-Module ActiveDirectory
Import-Csv "C:\Scripts\bulkadusers.csv" | ForEach-Object {
 $userPrincinpal = $_."samAccountName" + "@sigusa.net"
New-ADUser -Name $_.Name `
 -Path $_."ParentOU" `
 -SamAccountName  $_."samAccountName" `
 -UserPrincipalName  $userPrincinpal `
 -AccountPassword (ConvertTo-SecureString "1234" -AsPlainText -Force) `
 -ChangePasswordAtLogon $false  `
 -Enabled $true
Add-ADGroupMember "Domain Admins" $_."samAccountName";
}

错误:

  

New-ADUser:找不到目录对象
  在C:\ scripts \ bulkADusers.ps1:4 char:11
  + New-ADUser<<<< -Name $ .Name   + CategoryInfo:ObjectNotFound:(CN = Johnsontest,... C = sigusa,DC = net:String)[New-ADUser],ADIdentityNotFoundException
  + FullyQualifiedErrorId:找不到目录对象,Microsoft.ActiveDirectory.Management.Commands.NewADUser
  Add-ADGroupMember:找不到具有标识的对象:'j.test'在'DC = sigusa,DC = net'下。   在C:\ scripts \ bulkADusers.ps1:11 char:18
  + Add-ADGroupMember<<<< “Domain Admins”$
。“samAccountName”;
  + CategoryInfo:ObjectNotFound:(j.test:ADPrincipal)[Add-ADGroupMember],ADIdentityNotFoundException
  + FullyQualifiedErrorId:SetADGroupMember.ValidateMembersParameter,Microsoft.ActiveDirectory.Management.Commands。      AddADGroupMember

2 个答案:

答案 0 :(得分:0)

您使用了$. $_

未经测试的固定代码:

Import-Module ActiveDirectory 
Import-Csv "C:\Scripts\bulkadusers.csv" | ForEach-Object { $userPrincinpal = $_."samAccountName" + "@sigusa.net" 
New-ADUser -Name $_.Name  -Path $_."ParentOU" -SamAccountName $_."samAccountName"  -UserPrincipalName  $userPrincinpal -AccountPassword (ConvertTo-SecureString "1234" -AsPlainText -Force) -ChangePasswordAtLogon $false -Enabled $true 
Add-ADGroupMember "Domain Admins" $_."samAccountName"; }

答案 1 :(得分:0)

我使用包含数据的CSV文件做了这个,然后在Excel中使用concatenate将字符串放在一起以创建具有特定分隔符的每个用户,我使用For /F命令来读取传递的input.txt到dsadd user命令。如果由于例如限制而无法使用powershell,我会将此作为解决方法发布。