0

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

  • Try `{EmployeeID -like $ID}` (no quotes) (edit: just noticed you have it in script block but in first line of code you have it with quotes) – Robert Dyjas Jun 27 '18 at 14:42
  • [Related](https://stackoverflow.com/questions/25930811/using-filter-with-a-variable-in-powershell) – Robert Dyjas Jun 27 '18 at 14:44

1 Answers1

1

It looks like the problem could be the wildcard at the end of your $employee variable.

$employee = Get-ADUser -Filter {EmployeeID -eq $ID} -Properties (*)

I have a feeling that's why your variable is coming up null. (i.e. you may have to define what properties you are looking for, as the wildcard may be trying to pull all of them.)

**Edit: Alternatively, you could try placing ('') around your EmployeeID data search, from

$employee = Get-ADUser -Filter {EmployeeID -eq $ID} -Properties *

to

$employee = Get-ADUser -Filter {'EmployeeID' -eq $ID} -Properties *

Hope this helps lead you in the right direction!

iExist
  • 28
  • 1
  • 7
  • I don't have AD on my PowerShell system, but I ran a modified version against Get-Command with the wildcard at the end for properties, and it ran fine. You may instead have to pipe your results into another command, and set that as your variable, even though I'm not sure why that would be necessary, as it should be assigning that output to your variable. – iExist Jun 27 '18 at 14:45
  • Thank you, @iExist! This seemed to be the problem. My code is now `$employee = Get-ADUser -Filter {EmployeeID -eq $ID} -Properties surname | Select-Object surname` – Leah_CyberPadawan Jun 27 '18 at 14:49