mhmservices
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 ,Departmen t,physical DeliveryOf ficeName,S treetAddre ss,L,ST,po stalCode
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=$mg r}
}
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=$mg r}
+ CategoryInfo : WriteError: (:) [Set-ADUser], ParameterBindingEx
ception
+ FullyQualifiedErrorId : ParameterBindingFailed,Mic rosoft.Act iveDirecto ry
.Management.Commands.SetAD User
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- -
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!
I have a csv file that I use this to update all the AD attributes.
The CSV file looks like this:
EmployeeID,Title,managerID
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)
Get-ADUser -LDAPFilter "(employeeID=$_.employeeID
}
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
+ Get-ADUser -LDAPFilter "(employeeID=$_.employeeID
User -replace <<<< @{title=$title;manager=$mg
+ CategoryInfo : WriteError: (:) [Set-ADUser], ParameterBindingEx
ception
+ FullyQualifiedErrorId : ParameterBindingFailed,Mic
.Management.Commands.SetAD
--------------------------
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks, it worked.
Open in new window