none
Need a script (or two) that will set a users manager from an imported csv containing the EmployeeIDs of BOTH the user and the manager RRS feed

  • Question

  • I'm a newbie and still getting the hang of all this, appreciate the help!

    I need to BULK update the "Manager" field in AD (which I understand needs to be the DN value) for over 1000 employees.

    I've got a csv. file that contains the following fields (queried from HR psoft)

    EmployeeID, Title, Phone, Username(not SamAccountName), Email, LastName, GivenName, Location, SupervisorID

    *EmployeeID and SupervisorID are both populated with the appropriate user's EmployeeID -- I need to query for the "supervisors" DN and then use that value to SetADuser's  "Manager" name.

    I'd love to submit what I have here but I haven't had too much success yet, I've been cutting and pasting various scripts that come close but can't seem to get the right combinations.

    I'm happy with 1st generating a list of CN's from an edited csv file containing just the SupervisorID and then adding that column to the original csv file and going from there but I'd like to know I'm on the right track.

    Thanks again for any suggestions and for your time.

    Tedzo67  (Using PS or QuestAD)



    • Edited by Tedzo_67 Wednesday, December 14, 2016 10:57 PM
    Wednesday, December 14, 2016 7:46 PM

Answers

  • Please fix the title of your question.

    The manager field takes a distinguishedname and not a CN.

    Import-Csv is the command to run. See the help on how to use this.

    Get-AdUser -Filter "EmployeeID -eq '$($_.EmployeeID)'"

    will get you the user from the ID.


    \_(ツ)_/



    Wednesday, December 14, 2016 8:57 PM

All replies

  • Please fix the title of your question.

    The manager field takes a distinguishedname and not a CN.

    Import-Csv is the command to run. See the help on how to use this.

    Get-AdUser -Filter "EmployeeID -eq '$($_.EmployeeID)'"

    will get you the user from the ID.


    \_(ツ)_/



    Wednesday, December 14, 2016 8:57 PM
  • thanks for the reply jrv:

    I got the following:

    Get-ADUser : Error parsing query: 'EmployeeID=''' Error Message: 'Operator Not supported: =' at position: '11'.At line:2 char:1
    + Get-AdUser -Filter "EmployeeID='$($_.EmployeeID)'"

    Appreciate the help!

    I'm running

    Major  Minor  Build  Revision
    -----  -----  -----  --------
    4      0      -1     -1     

    Wednesday, December 14, 2016 11:20 PM
  • Sorry - typo.  I fixed the above example.


    \_(ツ)_/

    Wednesday, December 14, 2016 11:24 PM
  • Thanks jrv:

    So that gave me a display of the fields in my csv file (then generated the error below) but it didn't generate a DN value?

    Get-ADUser : The search filter cannot be recognizedAt line:2 char:1
    + Get-AdUser -Filter "EmloyeeID -eq '$($_.EmployeeID)'"

    Thanks,
    Tedzo67

    Wednesday, December 14, 2016 11:37 PM
  • Check spelling of Employee
    Wednesday, December 14, 2016 11:53 PM
  • Check spelling of Employee

    Thanks - missed that too.

    \_(ツ)_/

    Thursday, December 15, 2016 12:05 AM
  • You are Welcome
    Thursday, December 15, 2016 12:15 AM
  • So with the spelling corrections in place and the above script/cmdlet running against my csv I'm still only getting a screen parsing of the data in the file.  I'm literally seeing the same information on my screen that's contained in the file.

    I need to obtain the DN of the 2nd listed EmployeeID# or the Suprervisor's ID... and then use that data to update the Manager field.

    Thanks


    Cheers! Tedzo67

    Thursday, December 15, 2016 3:40 PM
  • So with the spelling corrections in place and the above script/cmdlet running against my csv I'm still only getting a screen parsing of the data in the file.  I'm literally seeing the same information on my screen that's contained in the file.

    I need to obtain the DN of the 2nd listed EmployeeID# or the Suprervisor's ID... and then use that data to update the Manager field.

    Thanks


    Cheers! Tedzo67

    Post your script.


    \_(ツ)_/

    Thursday, December 15, 2016 5:08 PM
  • Since there is no guarantee that employeeID will uniquely identify the user, I like to make sure one and only one user is found for each ID. I would use code similar to below:

    $Users = Import-Csv ".\users.csv"
    ForEach ($User In $Users)
    {
        $Employee = $User.EmployeeID
        $EmployeeDN = (Get-ADUser -Filter {employeeID -eq $Employee}).distinguishedName
        # Make sure the employee is found.
        If ($EmployeeDN)
        {
            $Manager = $User.SupervisorID
            $ManagerDN = (Get-ADUser -Filter {employeeID -eq $Manager}).distinguishedName
            # Make sure the manager is found.
            If ($ManagerDN)
            {
                Set-ADUser -Identity $EmployeeDN -Manager $ManagerDN
            }
            Else {"Manager not found or not unique $Manager"}
        }
        Else {"Employee not found or not unique: $Employee"}
    }
    

    This will skip the line in the CSV if either of the two ID's is either not found, or if it matches more than one user.

    I also should note that the manager attribute in AD must be a DN, but the -Manager parameter of Get-ADUser will accept either the DN or the sAMAccountName. It doesn't matter in this case, as a lookup would still be required.


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Thursday, December 22, 2016 4:06 PM
    Moderator