Updating AD from csv

Welcome Forums General PowerShell Q&A Updating AD from csv

This topic contains 3 replies, has 2 voices, and was last updated by

 
Participant
1 year, 1 month ago.

  • Author
    Posts
  • #98317

    Participant
    Topics: 2
    Replies: 5
    Points: 0
    Rank: Member

    Hello,
    I'm stuck and hopefully someone can give me some direction. Every 2 weeks I receive a .csv for HR. I use it to sync employee info to AD. I had a script that has been working:

    foreach ($user in $users) {            
    Get-ADUser -Filter "UserPrincipalName -eq '$($user.email)'" -Properties * -SearchBase "OU=someOU,DC=mydomain,DC=local" |            
      Set-ADUser -Division $($user.Division) -Department $($user.Department) -EmployeeNumber $($user.EmployeeNumber) -Title $($user.Job) 
    }

    I now need to add “manager” to AD. So, I did this:

    foreach ($user in $users) {            
    Get-ADUser -Filter "UserPrincipalName -eq '$($user.email)'" -Properties * -SearchBase "OU=someOU,DC=mydomain,DC=local" |            
      Set-ADUser -Division $($user.Division) -Department $($user.Department) -EmployeeNumber $($user.EmployeeNumber) -Title $($user.Job) -Manager $($user.Manager) 
    }

    However, ADUser -Manager only accepts 4 attributes. The only that makes sense for me is SamAccountName. Unfortunately, our SamAccountName does not match our UPN and it is not included in the .cvs I'm given. I do have the employee's manager's email address, which is the same as the UPN so, I did this this to get the SamAccountName of the employee's manager:

    foreach ($user in $users) {            
    Get-ADUser -Filter "UserPrincipalName -eq '$($user.manageremail)'" -SearchBase "DC=mydomain,DC=local" -Properties  SamAccountName | Select SamAccountName
     }

    This works and I get a list of each emplyee's mager's SamAccountName.
    However, I cannot figure out have to feed these results into -Manager $($user.Manager)
    Everything I tried has failed. Mostly because I'm not sure of the correct approach. Any help would be greatly appreciated!

  • #98319

    Participant
    Topics: 4
    Replies: 52
    Points: 49
    Rank: Member

    If you want an inline solution:

    foreach ($user in $users) {            
    Get-ADUser -Filter "UserPrincipalName -eq '$($user.email)'" -Properties * -SearchBase "OU=someOU,DC=mydomain,DC=local" |            
      Set-ADUser -Division $($user.Division) -Department $($user.Department) -EmployeeNumber $($user.EmployeeNumber) -Title $($user.Job) -Manager $((Get-ADUser -Filter "UserPrincipalName -eq '$($user.manageremail)'" -SearchBase "DC=mydomain,DC=local" -Properties  SamAccountName).SamAccountName) 
    }

    Otherwise, for readability, you can do this:

    foreach ($user in $users) {            
    $Manager = (Get-ADUser -Filter "UserPrincipalName -eq '$($user.manageremail)'" -SearchBase "DC=mydomain,DC=local" -Properties  SamAccountName).SamAccountName
    Get-ADUser -Filter "UserPrincipalName -eq '$($user.email)'" -Properties * -SearchBase "OU=someOU,DC=mydomain,DC=local" |            
      Set-ADUser -Division $($user.Division) -Department $($user.Department) -EmployeeNumber $($user.EmployeeNumber) -Title $($user.Job) -Manager $Manager
    }
    • #98322

      Participant
      Topics: 2
      Replies: 5
      Points: 0
      Rank: Member

      OK, thanks, Jeremy. I'll give that a try.

    • #98625

      Participant
      Topics: 2
      Replies: 5
      Points: 0
      Rank: Member

      That worked perfectly. Thanks so much for your help.

The topic ‘Updating AD from csv’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort