TransWikia.com

Update employee manager using employeeid

Stack Overflow Asked by user8624315 on December 19, 2020

So, I am working on a project that will update some employee information weekly as required by audit based on our HR system. The only unique matching field that I have from HR is employeeID (for both users and managers). I have no problem getting Get-ADUser to pull up the employee and update these fields, but now I also need to update the employee’s manager using their employee ID. I’m not sure how to get powershell to convert that manager employee ID into their UPN or some other method to pull their info. Here is my code

Import-Module ActiveDirectory

$Users = Import-csv "C:Usersmy.stuffstuffTitlePowershell.csv"

foreach($User in $Users) {
    Get-ADUser -Filter {EmployeeID -eq $user.employeeID} 
        | Set-ADUser -replace @{description=$User.description; title=$User.title; manager=$user.ManagerID}
}

What can I add to get manager to update from their employee id?

One Answer

The Manager attribute of a user contains the DistinguishedName of that user.
Since apparently in your CSV file, you have the EmployeeID of that manager, you need to do some extra work to get the DN

Try

Import-Module ActiveDirectory

$Users = Import-csv "C:Usersmy.stuffstuffTitlePowershell.csv"

foreach($User in $Users) {
    # get the user object for the manager
    $usr = Get-ADUser -Filter "EmployeeID -eq '$($user.employeeID)'" -ErrorAction SilentlyContinue
    if ($usr) {
        # get the user object for the manager
        $mgr = Get-ADUser -Filter "EmployeeID -eq '$($user.ManagerID)'" -ErrorAction SilentlyContinue
        if ($mgr) {
            $usr | Set-ADUser -Replace @{Description = $User.description; Title = $User.title; Manager = $mgr.DistinguishedName}
        }
        else {
            Write-Warning "No manager found with EmployeeID $($user.ManagerID)"
        }
    }
    else {
        Write-Warning "No user found with EmployeeID $($user.employeeID)"
    }
}

Correct answer by Theo on December 19, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP