导入AD Powershell的脚本

时间:2018-03-29 00:16:51

标签: powershell csv active-directory

我在使用Active Directory的导入用户脚本时遇到问题。它之前工作我添加了员工ID,地址和SamAccountName现在我收到了错误的语法错误,我可以得到一些帮助。

脚本:

# Create password for users
$Password = ConvertTo-SecureString "Bevchain123$" -AsPlainText -Force


# Inputing CSV file path
$CSVlocation = Read-Host -Prompt "Enter path to CSV file"

# Puttting file into a variable
$users = Import-Csv $CSVlocation 




#Create for each function to grab data from CSV file

foreach ($user in $users)  {

#Frist name
$FirstName = $user.'First Name'  

#Last name
$LastName = $user.'Last Name' 

#description
#$Description = $user.Description

#Orginisational Unit Path
$OUpath = $user.Unit 

$EmployID = $user.'Personal Number'

$Address = $user.Address

$aaccount = $user.Samaccountname

# Create the Active Directory user for each line of the CSV

NEW-ADUser -Name "$FirstName $LastName" -EmployeeID "$EmployID" -SamAccountName "$aaccount" -GivenName "$FirstName" -StreetAddress "$Address" -UserPrincipalName "$FirstName.$LastName"  -AccountPassword $Password -ChangePasswordAtLogon $false -Path $OUpath


# Message output to screen

Echo "user created successfully for: $FirstName in path: $OUpath"

}

错误讯息:

NEW-ADUser : The object name has bad syntax
At C:\Users\Administrator\Desktop\Scripts\Import Users.ps1:41 char:1
+ NEW-ADUser -Name "$FirstName $LastName" -EmployeeID "$EmployID" -SamA ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (CN=Brendan Week...vTest,DC=Local":String) [New-ADUser], ADException
    + FullyQualifiedErrorId : ActiveDirectoryServer:8335,Microsoft.ActiveDirectory.Management.Commands.NewADUser

CSV格式:

First Name,Last Name,Address,Personal Number,Unit,Samaccountname
Test,User01,123 Test Street,000001,"OU=TestOU,DC=TestDC,DC=Local",Tuser01
Test,User02,123 Test Street,000002,"OU=TestOU,DC=TestDC,DC=Local",Tuser02

0 个答案:

没有答案