Leah_CyberPadawan
Leah_CyberPadawan

Reputation: 35

Empty (null) PowerShell Variable

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

Upvotes: 0

Views: 664

Answers (1)

iExist
iExist

Reputation: 38

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!

Upvotes: 1

Related Questions