Link to home
Start Free TrialLog in
Avatar of mhmservices
mhmservicesFlag for United States of America

asked on

Using Powershell to Update "manager" attribute in AD

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!
ASKER CERTIFIED SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Also, you can just use the -manager parameter without issue.
Get-ADUser -LDAPFilter "(employeeID=$_.employeeID)" -properties * | Set-ADUser -manager $mgr

Open in new window

Avatar of mhmservices

ASKER

Thanks, it worked.