0

I am trying find a specific user in AD using samaccountname if i type in an incorrect account i want the question to repeat until it finds the user, tells me the username and pauses until a key is pressed. Here is what i have so far and its not working. I am still a noob at this.

do{
$User=Read-Host "Enter SamAcountname"}

if (dsquery user -samid $User)
{
trap {$_ | write-host "Found user $_"
}
else 
{
trap {$_ | write-host "User not found!" 
}
{
until ( $_-eq "found user")
}

Write-Host "Press any key to continue ..."

$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
leppie
  • 115,091
  • 17
  • 196
  • 297
user2402045
  • 71
  • 1
  • 3
  • 13

1 Answers1

0

This should do it:

$prompt = "enter samaccountname"

do{    
    $User = Read-Host $prompt
} while (!$(dsquery user -samid $User;$prompt="User '$user' was not found, try again"))

$User
Shay Levy
  • 121,444
  • 32
  • 184
  • 206
  • that worked awesome! I understand everything except (!$(dsquery and $User;$prompt. what is this specifying? Thanks! – user2402045 Jun 10 '13 at 20:19
  • the while block should have read: (!(dsquery and $User)) which basically checks if there was an output and negats the answer so the criteria can be evaluated to: as long as there no output re-prompt, The $prompt assignment is updating the prompt text. If you put both commands inside while it will fail, $() is a workaround to allow both commands to run. – Shay Levy Jun 10 '13 at 20:24