Solved

Using Powershell to Update "manager" attribute in AD

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
Active Directory security has been a hot topic of late, and for good reason. With 90% of the world’s organization using this system to manage access to all parts of their IT infrastructure, knowing how to protect against threats and keep vulnerabil…
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 …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

729 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