Home > Enterprise >  About importing manager attribute to AD from a CSV
About importing manager attribute to AD from a CSV

Time:11-28

I'm trying to import manager attribute to active directory for set of users using the following CSV file template

GivenName   Surname DisplayName Department  Title      mail      MobilePhone    Manager SamAccountName
John        Smith   John Smith  IT          IT Manager john@example.com 1234   Mark Ebert JohnS

I used the below script and but it throws out an error.What i'm thinking it is due to manager attribute required to be in distinguished name format and **but i cannot change the csv manager column name as it comes from a different program.**The manager name in the CSV file shows in first name and last name format. What i need is to import the data on it to AD like the way it is.Any alternative methods available for this scenario.Here is the example script i used.

# Import AD Module           
Import-Module ActiveDirectory           strong text
$users = Import-Csv -Path C:\temp\MergedTo_AD.csv                     
foreach ($user in $users) 
{Get-ADUser -Filter "SamAccountName -eq '$($user.samaccountname)'" | Set-ADUser -GivenName $($User.GivenName) -Surname $($User.Surname) -DisplayName $($User.DisplayName) -title $($User.title) -EmailAddress $($User.EmailAddress) -MobilePhone $($User.MobilePhone) $User -manager $ID }
 

CodePudding user response:

I'm not a scripting expert at all.I amended the script as below as per your suggestion.

# Import AD Module           
Import-Module ActiveDirectory           
$users = Import-Csv -Path C:\temp\MergedTo_AD.csv  
{Get-ADUser -Filter "SamAccountName -eq '$($user.samaccountname)'" $FirstName,$LastName = (-split $User.Manager).Trim() $ID = (Get-ADUser -LDAPFilter "(&(GivenName=*$FirstName*)(SurName=*$LastName*))").SamAccountName (Get-ADUser -LDAPFilter "(&(GivenName=*$FirstName*)(SurName=*$LastName*))").SamAccountName| Set-ADUser -GivenName $($User.GivenName) -Surname $($User.Surname) -DisplayName $($User.DisplayName) -title $($User.title) -EmailAddress $($User.EmailAddress) -OfficePhone $($User.OfficePhone) -MobilePhone $($User.MobilePhone) -manager $($User.manager) }

I'm getting below error

Get-ADUser : The search filter cannot be recognized
At C:\Temp\PowershellScript-Users Import.ps1:5 char:128
  ... im() $ID = (Get-ADUser -LDAPFilter "(&(GivenName=*$FirstName*)(SurNam ...
                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      CategoryInfo          : NotSpecified: (:) [Get-ADUser], ADException
      FullyQualifiedErrorId : ActiveDirectoryServer:8254,Microsoft.ActiveDirectory.Management.Commands.GetADUser

CodePudding user response:

I've reviewed the code and the reason it fails is because you cannot add the manager using the variable '$ID' is that it has no reference, nor does it resolve to the managers active directory user account. Your choices are either add the managers Distinguished Name to your csv file or stick it in your code to resolve the managers Distinguished Name.

#Import CSV File to set variable for the user’s logon name of whom manager’s field needs to be filled   delimiter
$users = Import-Csv -Delimiter ";" -Path  "C:\temp\MergedTo_AD.csv"

foreach ($user in $users) {
    #The Managers AD Sam Account Name
    $ManagersaMACCount = "saMACcount"
    
    #The Managers AD Distinguished Name
    $ManagerID = (Get-ADUser -identity $ManagersaMACCount).DistinguishedName
    
    #Example of Setting User's Manager Attribute -Example below
    #Get-aduser -identity $user | Set-ADUser -Manager $ManagerID
    
    #Using your code to filter AD Sam Accounts Based on column samaccountname in the csv file
    Get-ADUser -Filter "SamAccountName -eq '$($user.samaccountname)'" `
    #Pipe Set Users GivenName Based on column GivenName
    | Set-ADUser -GivenName $($User.GivenName) `
    #Set Users Surname Based on column Surname
    -Surname $($User.Surname) `
    #Set Users Display Name Based on column DisplayName
    -DisplayName $($User.DisplayName) `
    #Set Users Title Based on column Title
    -title $($User.title) `
    #Set Users Email Address Based on column EmailAddress
    -EmailAddress $($User.EmailAddress) `
    #Set Users Mobile Phone Based on column MobilePhone
    -MobilePhone $($User.MobilePhone) `
    #Set Users Manager Based on the Distinguished Name Attribute In Active Directory
    -manager $ManagerID
}
  • Related