I'm working on a script for my employer that will compare what we have in Active Directory with a CSV file from another software that we use. Active Directory should match the CSV file. The problem I am running into is that the $emplyee variable is always coming up empty (null):
$employee = Get-ADuser -Filter {EmployeeID -like "$ID"} -properties *
The $employee variable is the Active Directory account that should be compared to the current $user in the foreach loop. While troubleshooting with the Debugger from VS Code, I discovered that the $employee variable is blank, so the script is unable to compare the two objects. Below is the code I am using:
$users = Import-Csv C:\Users\Administrator\Desktop\June18Report_2.csv
foreach ($user in $users) {
$ID = $user.'Employee ID'
$employee = Get-ADUser -Filter {EmployeeID -eq $ID} -Properties *
$lastname = $user.'Last Name'
$samaccountname = $employee.SamAccountName
if ($lastname -ne $employee.surname) {
Write-Host "The last name was changed for $samaccountname" -ForegroundColor Red
get-aduser -Identity $employee.SamAccountName | Set-ADUser -Surname $lastname
} Else {
Write-Host "The last name is correct for $samaccountname" -ForegroundColor Green
}
Any help or tips for this problem would be greatly appreciated!
Thank you! - leah_cyberpadawan