Home > Blockchain >  Powershell - Update field in AD from 2 txt files
Powershell - Update field in AD from 2 txt files

Time:10-25

I'm using Powershell to update the manager attribute in AD using data from 2 txt files.

One txt file has employee IDs in one column their manager's ID in the other like this:

1111 2222
4444 3333

The other txt file has all employee info listed by IDs like this:

1111 POTTER HARRY HPOTTER 200 3108675309 
2222 GRANGER HERMIONE HGRANGER 201 3107942312

HPOTTER and HGRANGER are the samaccountnames. I want to use the IDs to grab all the samaccountnames to update the manager attribute for each employee.

There are several employeeIDs listed in the first txt file that are not listed in the second txt file (they are no longer employed). I thought that wouldn't really matter, but what I wrote so far hasn't been updating every employee correctly. I rewrote it and am stuck on how to loop through $employees to get both the employee samaccountname and the manager samaccountname.

$employees = Get-Content -Path 'Staff.txt'
$supervisors = Get-Content -Path 'Supervisors.txt'

foreach($supervisor in $supervisors){
    $employeeID = $supervisor.substring(0,7)
    $managerID = $supervisor.substring(8,7)

    foreach($employee in $employees){
        if ($employee.contains($employeeID)){
            $empsamName = $employee.split("`t")[3]

            #some code to get the manSamName

            $ADUser = Get-ADUser -Filter "samaccountname -like '$empSamName'"
            $manager = Get-ADUser -Filter "samaccountname -like '$manSamName'"

                if($ADUser -and $manager){
                $ADUser | Set-ADUser -manager $manager
                }
        }
    }
}

Thanks for the help!

CodePudding user response:

Instead of running a loop through each line to check if it matches the employee/manager ID, uou would want to use Where-Object to find a match.

Example

$employees | Where-Object {$_ -like "*1111*"}

I've provided a full solution below that's a little more thorough, allowing you to reference each employee as a separate object, and their attributes using dot-notation


Result

Line 1

EmployeeID EmployeeSamAccountName ManagerID Manager
1111 HPOTTER 2222 HGRANGER

Line 2

Employee ID 4444 no longer employed

EmployeeID EmployeeSamAccountName ManagerID Manager
4444 3333

Code

$employees = Get-Content -Path 'Staff.txt'
$supervisors = Get-Content -Path 'Supervisors.txt'

$employeeTable = foreach ($employee in $employees) {

    # From the staff file, split each column into an array
    $employeeSplit = $employee.Split(" ")

    # Create a table from all the split columns, and add this employee object into another array (the $employeeTable)
    [PSCustomObject] @{
        EmployeeID              = $employeeSplit[0]
        EmployeeFirstName       = $employeeSplit[1]
        EmployeeSurname         = $employeeSplit[2]
        EmployeeSamAccountName  = $employeeSplit[3]
        Manager                 = "" # Leave this blank #
        Column5                 = $employeeSplit[4]
        Column6                 = $employeeSplit[5]
    }
}

foreach ($supervisor in $supervisors) {

    # From the supervisor file, split the 2 columns into separate variables
    $employeeID, $managerID = $supervisor.Split(" ")

    # From the employee table, get the employee that matches the ID from column 1 of the supervisors file
    $employee = ($employeeTable | Where-Object { $_.EmployeeId -eq $employeeId } )

    # Continue if the employee is still employed
    if ($employee -ne $null) {
        
        # Update the employee table to add the manager's name
        $employee.Manager = ($employeeTable | Where-Object { $_.EmployeeId -eq $managerID } ).EmployeeSamAccountName

        # Do your stuff
        $ADUser = Get-ADUser -Filter "samAccountName -eq '$($employeeTable.EmployeeSamAccountName)'"
        $manager = Get-ADUser -Filter "samAccountName -eq '$($employeeTable.Manager)'"
    }

    # Build a new table for the supervisor file
    $outputTable = [PSCustomObject] @{
        EmployeeID             = $employeeID
        EmployeeSamAccountName = $($employee.EmployeeSamAccountName)
        ManagerID              = $managerId
        Manager                = $($employee.Manager)
    }

    # Sample output
    Write-Host "Line $($supervisors.IndexOf($supervisor))" -BackgroundColor DarkCyan
    if ($employee.EmployeeSamAccountName -eq $null) {
        Write-Host "Employee ID $($employeeID) no longer employed" -ForegroundColor Red
    }
    Write-Host ($outputTable | ft | Out-String)
    
}
  • Related