Powershell Set-ADUser error

Import-module ActiveDirectory
# Initialize variables for progress bar
$i = 0
$pathtocsvfile = 'C:\Import\Scripts\UserInfo\140506.csv'
$max = ((Get-Content $pathtocsvfile).count -1)
$csv = import-csv $pathtocsvfile -Delimiter ','
# Import loop for transferring data from CSV file to Active Directory accounts
ForEach ($line in $csv)
{
$user = Get-ADUser $line.SamAccountName -Properties employeeID,employeeNumber,employeeType
$user.employeeID = $line.EmployeeNumber
$user.employeeNumber = $line.EmployeeNumber
$user.employeeType = $line.EmployeeType
    Set-ADUser -instance $user
$i++
}

The above script works fine but the one below gives an error.

Import-module ActiveDirectory
# Initialize variables for progress bar
$i = 0
$pathtocsvfile = 'C:\Import\Scripts\UserInfo\Test150821.csv'
$max = ((Get-Content $pathtocsvfile).count -1)
$csv = import-csv $pathtocsvfile -Delimiter ','
# Import loop for transferring data from CSV file to Active Directory accounts
ForEach ($line in $csv)
{
$user = Get-ADUser $line.SamAccountName -Properties title,Manager
$user.title = $line.TitleColl
$user.Manager = $line.Supervisor
      Set-ADUser -instance $user
$i++
}

The error is:

Set-ADUser : The name reference is invalid
At C:\Import\Scripts\UserInfo\ERW-Title-Supervisor.ps1:18 char:2
+     Set-ADUser -instance $user
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Set-ADUser], ADException
    + FullyQualifiedErrorId : The name reference is invalid,Microsoft.ActiveDirectory.Management.Commands.SetADUser

I have gone over both scripts and can't see find the discrepancy causing this but most likely am missing something that is fairly obvious from an objective view.

Any suggestions?

Thanks.
IndwesAdminAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

systechadminConsultantCommented:
kindly try the code

Set-ADUser  $user
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
IndwesAdminAuthor Commented:
I can accept this as a solution for getting rid of the error but the fields in AD are not being updated.  Are there any suggestions on what I need to alter in order to update the Manager and Job Title fields?

Thanks.
0
systechadminConsultantCommented:
You will need to check the attributes of AD part and what field you have defined in your CSV file?
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

systechadminConsultantCommented:
it seems that attributes are not fetching up from CSV properly.
0
IndwesAdminAuthor Commented:
I have confirmed that the information is coming in from the CSV successfully by inserting a write-host line in the loop.  They are still not writing to the AD accounts.
0
footechCommented:
@IndwesAdmin - you shouldn't accept a solution until you have verified it works.  You can request attention to the question to have a moderator reopen it.

To set the manager in this fashion (using the -Instance parameter) you have to supply the complete DistinguishedName of the manager account (like "CN=John Doe,OU=Users,DC=domain,DC=com").  It's a bit odd, because that contradicts what is shown in the help for the cmdlet.  If you want to just supply the samAccountName in the .CSV, then you need to do some query to lookup the DN or use Set-ADUser with the -Manager parameter (where it will accept samAccountName, GUID, or DN).
1
IndwesAdminAuthor Commented:
Thank you.  The reason for the (partial) acceptance was that the suggestion did get rid of the error even if it didn't solve the issue of not updating the fields on the Organization tab in AD.

The information I have been given for the CSV is "SamAccountName,Title,CN" ... I will work on getting the DN from the CN.

Thanks again.
0
IndwesAdminAuthor Commented:
Thanks for all the suggestions.  There comes a point where I appreciate any fresh input I can get.

I was able to get the script to update the fields by massaging one of my CSV fields, looking up the DN in AD and using that to fill the "Manager" field.  After confirming all of the correct field information  I was able to reinsert the "Set-ADUser -instance $user" line and successfully populate the desired fields on the Organization tab.  I am gradually learning the ins and outs of this forum and attempting to find the nuances of the system.

Thanks for the help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.