question

JesusLopezMancilla-2223 avatar image
1 Vote"
JesusLopezMancilla-2223 asked RichMatheisen-8856 commented

Updating Updating Active Directory Manager Attribute using PS from a CSV File

I'm trying to update the AD manager field using PS. We we're given a CSV file from HR with everyone's manager info. This is PS script.


Import-Module ActiveDirectory
$Users = Import-Csv "C:\Users\Administrator.APC-SERVICES\Desktop\mngimport\Managers1.csv"
ForEach ($User in $User)
{
$ADUser = Get-ADUser -Filter "displayname -eq '$($User.ProfessionalFullName)'"
$manager = (Get-ADUser -Filter "displayname -eq '$($User.'ManagerFullName')'").distinguishedname

if ($ADUser -and $manager) {
Set-ADUser -Identity $ADUser -Replace @{manager = $manager }
}
}


Nothing happens after running this. I keep checking the AD Users and the Manager attribute does not update. What am i missing?

CSV format:
ProfessionalFullName,ManagerFullName
Greg Brown,Jay Smith
Jesus Lopez ,Jay Smith
Stephen Jones,Jay Smith
Scott Williams ,Roy Miller
Jason Davis,Roy Miller

windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
2 Votes"
RichMatheisen-8856 answered JesusLopezMancilla-2223 commented

Try it this way:

 Import-Csv "C:\Users\Administrator.APC-SERVICES\Desktop\mngimport\Managers1.csv" |
     ForEach-Object {
         $ADUser  =  Get-ADUser -Filter "displayname -eq '$($_.ProfessionalFullName)'"
         $manager = (Get-ADUser -Filter "displayname -eq '$($_.ManagerFullName)'").distinguishedname
    
         if ($ADUser -and $manager) {
             $ADUser | Set-ADUser -manager $manager
         }
     }

Two things:

  1. You don't have to explicitly import the ActiveDirectory module. PowerShell (since version 3) will do that automatically.

  2. Using the DisplayName to identify users is a little bit risky since a display name may not be unique in the AD forest. A samaccountname or primary email address is much better

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Agreed on the samaccountname i should of used that to start with.

1 Vote 1 ·

Hey this is what got me going. Using the $fullName = $User.ProfessionalFullName made a lot of sense.

Import-Module ActiveDirectory
$Users = Import-Csv "C:\Users\Administrator.APC-SERVICES\Desktop\mngimport\Managers1.csv"

ForEach ($User in $Users){
}
$fullName = $User.ProfessionalFullName
$ADUser = Get-ADUser -Filter {displayName -eq $fullName}
$managerFullName = $user.ManagerFullName
$manager = Get-ADuser -Filter {displayName -eq $managerFullName}

If ($ADUser -and $manager){
$managerDistName = $Manager.DistinguishedName
Set-ADUser -identity $ADUser -Manager $Manager.DistinguishedName

}


Thanks for your help here!


0 Votes 0 ·
JesusLopezMancilla-2223 avatar image
0 Votes"
JesusLopezMancilla-2223 answered RichMatheisen-8856 commented

Hey,

Is look like half of my users failed to updated. I modified it for "sAMAccountName" and the "Name".

This is what I'm running:

Import-Csv "C:\Users\Administrator.APC-SERVICES\Desktop\mngimport\managers.csv" |
ForEach-Object {
$ADUser = Get-ADUser -Filter "Name -eq '$($.Name)'"
$manager = (Get-ADUser -Filter "sAMAccountName -eq '$($
.sAMAccountName)'").distinguishedname
}
if ($ADUser -and $manager) {
$ADUser | Set-ADUser -manager $manager
}


CSV File:
Name,sAMAccountName
Dipal Smith,VBell
Dolly Ryan,VBell

Any thoughts?

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

If that's the code that you ran the only user that would be update would be last one in the CSV!

The last three lines you posted belong immediately after the "$manager = (Get-ADUser -Filter . . .", and not after the "}" on the line below it. IOW, it should look like the code I posted (with the property name substitutions you made, of course).

0 Votes 0 ·