将用户添加到Active Directory

时间:2016-09-27 13:09:21

标签: powershell active-directory

我尝试使用下面的脚本向Active Direcroty添加新用户但由于某种原因我不断收到错误消息:

剧本:

Import-Module ActiveDirectory
Import-Csv 'C:\Scripts\\AddUsers.csv' -Delimiter "," | ForEach-Object {
 $userPrincinpal = $_."SAM" + "@domain.org"
New-ADUser 
 -Name $_.Name `
 -GivenName $_."First_Name" `
 -Surname $_."Last_Nimpoame" `
 -Description "Student"
 -Path $_."OU" `
 -SamAccountName  $_."SAM" `
 -UserPrincipalName  $userPrincinpal `
 -AccountPassword (ConvertTo-SecureString "password2016" -AsPlainText -Force) `
 -ChangePasswordAtLogon $true  `
 -Enabled $true
}

Write-Host "Done!"

错误消息:

-Name : The term '-Name' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, 
verify that the path is correct and try again.
At C:\Scripts\Add Bulk AD User CSV\add_ad_users2.ps1:5 char:2
+  -Name $_.Name `
+  ~~~~~
    + CategoryInfo          : ObjectNotFound: (-Name:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

-Path : The term '-Path' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, 
verify that the path is correct and try again.
At C:\Scripts\Add Bulk AD User CSV\add_ad_users2.ps1:9 char:2
+  -Path $_."OU" `
+  ~~~~~
    + CategoryInfo          : ObjectNotFound: (-Path:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

我不确定为什么我一直收到这些错误消息,因为我已经导入了ActiveDirectory模块。

你能帮忙吗?!

1 个答案:

答案 0 :(得分:2)

New-ADUser

之后,您错过了一个尾随反引号
Import-Module ActiveDirectory
Import-Csv 'C:\Scripts\\AddUsers.csv' -Delimiter "," | ForEach-Object {
 $userPrincinpal = $_."SAM" + "@domain.org"
New-ADUser `
 -Name $_.Name `
 -GivenName $_."First_Name" `
 -Surname $_."Last_Nimpoame" `
 -Description "Student"
 -Path $_."OU" `
 -SamAccountName  $_."SAM" `
 -UserPrincipalName  $userPrincinpal `
 -AccountPassword (ConvertTo-SecureString "password2016" -AsPlainText -Force) `
 -ChangePasswordAtLogon $true  `
 -Enabled $true
}

Write-Host "Done!"