My aim is to import users in a CSV file to Active Directory via a simple PowerShell Script. Despite this I'm encountering a syntax error as seen below.
Updated: CSV Column Format
name,surname,ou Steven,Boone,Management Rodney,Fisher,Sales Taylor,Bautista,Management Nathan,Morris,Management
Working and Solved: PowerShell Code
Import-Module ActiveDirectory
$ADDSUsers = Import-Csv C:\0469697M_gxt.csv
foreach ($user in $ADDSUsers) {
$Name = $user.name + " " + $user.surname
$OU = $user.ou
$OUPath = "OU=$($OU),dc=intgxt,dc=allaboutfood,dc=com,dc=mt"
#Creating New AD Users
New-ADUser -Name $Name -Path $OUPath
}
The error
New-ADUser : The object name has bad syntax At C:\Script.ps1:9 char:5 + New-ADUser -Name "$name" -Path "$OU" + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (CN=Tyler Blair,Management:String) [New-ADUser], ADException + FullyQualifiedErrorId :ActiveDirectoryServer:8335,Microsoft.ActiveDirectory.Management.Commands.NewADUser
Not sure where I have an error. As far as I'm concerned the columns are being parsed well.
Thank guys I managed to perfect my script and got it all working.
How I resolved the issue
As said above, I had to use X.500 path format for the script to find where to enter the users. For people having the same issues as me, here is the link which helped me along with the above: https://serverfault.com/questions/581383/new-aduser-path-syntax
I also have to use -Name rather than -GivenName , and then append $user.name and $user.surname together so that the names in the OU display as Dave Smith for example. This varies depending on your requirements.
Also, $($OU) had to be declared so that the script knows the OU for each corresponding user. (Provided in the .csv)
How to enter the right path (X.500 Path Format)
For anyone encountering the issue and stumbles upon this post.
Dots within Domain name are separated by dc= For example:
The Code
Special thanks to (Dave)https://stackoverflow.com/users/9712731/dave and (EBGreen)https://stackoverflow.com/users/1358/ebgreen for their guidance.