Solved

Using Powershell to Update "manager" attribute in AD

Posted on 2014-12-09
3
2,645 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
  • 2
3 Comments
 
LVL 39

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 39

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

Join & Write a Comment

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
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…
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 to another domain controller. Log onto the new domain controller with a user account t…

762 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now