-1

我正在嘗試使用PowerShell創建使用以下腳本的批量用戶,但是我收到了以下所述的錯誤。請幫忙!AD中的批量用戶

腳本:

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 : Directory object not found
At C:\scripts\bulkADusers.ps1:4 char:11
+ New-ADUser <<<< -Name $.Name + CategoryInfo : ObjectNotFound: (CN=Johnsontest,...C=sigusa,DC=net:String) [New-ADUser], ADIdentityNotFoundException
+ FullyQualifiedErrorId : Directory object not found, Microsoft.ActiveDirectory.Management.Commands.NewADUser
Add-ADGroupMember : Cannot find an object with identity: 'j.test' under: 'DC=sigusa,DC=net'. At 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

回答

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"; } 
+0

確定工作,但現在它給了我一個新的錯誤 新的ADUser:一個位置參數找不到接受參數'Add-ADGroupMember'。 在C:\ scripts \ bulkADusers.ps1:3 char:1 + New-ADUser -Name $ _。Name -Path $ _。「ParentOU」-SamAccountName $ _。「samAccountName ... + ~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~ CategoryInfo:InvalidArgument:(:) [New-ADUser],ParameterBindingException + FullyQualifiedErrorId:PositionalParameterNotFound,Microsoft。 ActiveDirectory.Management.Commands.NewADUser – 2014-12-06 18:24:09

+0

是的,'Add-ADGroupMember'需要在一個新行上,在答案中修復 – 2014-12-06 18:26:37

0

我這樣做是使用CSV與數據文件,然後在Excel中使用串連放在一起的字符串創建與我有For /F使用一個特定的符每個用戶命令來讀取傳遞給dsadd user命令的input.txt。如果powershell由於例如限制而不可用,我將其作爲解決方法發佈。