Solved

Using Powershell to Update "manager" attribute in AD

Posted on 2014-12-09
3
3,295 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 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article demonstrates probably the easiest way to configure domain-wide tier isolation within Active Directory. If you do not know tier isolation read https://technet.microsoft.com/en-us/windows-server-docs/security/securing-privileged-access/s…
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

685 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