Solved

Using Powershell to Update "manager" attribute in AD

Posted on 2014-12-09
3
3,424 Views
Last Modified: 2014-12-10
Hello,

I have a csv file that I use this to update all the AD attributes.

The CSV file looks like this:
EmployeeID,Title,managerID,Department,physicalDeliveryOfficeName,StreetAddress,L,ST,postalCode
11111,Engineer,22222,My Department,"111 main st., Suite 105",somecity,MD,22244

Below is the script I use to update the AD attributes:
Import-Module ActiveDirectory
Import-Csv update.csv | ForEach `
{
$mgr=Get-ADUser -LDAPFilter "(employeeID=$_.managerID)" | Select -ExpandProperty DistinguishedName
Get-ADUser -LDAPFilter "(employeeID=$_.employeeID)" -properties * |Set-ADUser -replace @{title=$title;manager=$mgr}
}

If I remove manager=$mgr the script works with no problems.

If I put it back it fails with error message:
----------------------------------------------------------------------------------------------------------------------------
Set-ADUser : Cannot bind parameter 'Replace' to the target. Exception setting "
Replace": "Object reference not set to an instance of an object."
At C:\temp\ad\userInfo.ps1:16 char:91
+     Get-ADUser -LDAPFilter "(employeeID=$_.employeeID)" -properties * |Set-AD
User -replace <<<<  @{title=$title;manager=$mgr}
    + CategoryInfo          : WriteError: (:) [Set-ADUser], ParameterBindingEx
   ception
    + FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.ActiveDirectory
   .Management.Commands.SetADUser
-------------------------------------------------------------------------------------------------------------------------------

I tried putting quotes around $mgr but it still did not work.  
The weird part is if I type one by one on the powershell window it works fine.

Any ideas what the issue could be?

Thanks!
0
Comment
Question by:mhmservices
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 40

Accepted Solution

by:
footech earned 500 total points
ID: 40490134
I don't know why it's different here, but the following works for me.  It seems that it is not detecting the variable as a string properly when trying to set the manager attribute.  I don't see the same issue when setting the title.
Get-ADUser -LDAPFilter "(employeeID=$_.employeeID)" -properties * |Set-ADUser -replace @{title=$title;manager="$mgr"}
# as does
Get-ADUser -LDAPFilter "(employeeID=$_.employeeID)" -properties * |Set-ADUser -replace @{title=$title;manager=[string]$mgr}

Open in new window

0
 
LVL 40

Expert Comment

by:footech
ID: 40490160
Also, you can just use the -manager parameter without issue.
Get-ADUser -LDAPFilter "(employeeID=$_.employeeID)" -properties * | Set-ADUser -manager $mgr

Open in new window

0
 

Author Closing Comment

by:mhmservices
ID: 40492770
Thanks, it worked.
0

Featured Post

Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Always backup Domain, SYSVOL etc.using processes according to Microsoft Best Practices. This is meant as a disaster recovery process for small environments that did not implement backup processes and did not run a secondary domain controller that ne…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question