Get-ADUser in function returns nothing on first run, then returns double values

17 Views Asked by At

I'm trying to get a simple function with get-aduser to work by filtering name like **. When I run the function the first time the return is blank. When I run it a second time it returns a double value. (only one user is present in AD)
e.g.:
First run: Nothing.
Second Run:

Name     SamAccountName  EmployeeID
----     --------------  ----------
John Doe john.doe        1111111
John Doe john.doe        1111111

Here's the code:

function UserLookup {
$NameFirst = Read-Host "Please type in a First name"
$NameLast = Read-Host "Please type in a Last Name"
$NameLookup = "*$NameFirst* *$NameLast*"
$FormatEnumarationLimit = -1
Write-Host "Looking for "$NameLookup
Get-ADUser -SearchBase "DC=Emea" -Filter {name -like $NameLookup} -Properties * | Select name, SamAccountName, EmployeeID
}

function mn7{
Clear-Host
UserLookup
#Write-Host "USER:" $UserLookup
$answer = Read-Host "Press M to go to main menu, R to retry."
    while ("M","R" -notcontains $answer)
        {
            $answer = Read-Host "Press M to go to main menu, R to retry."
        }
    if ($answer -eq 'M'){Continue :menu}
    Else {mn7}
}

mn7
0

There are 0 best solutions below