RJ96
RJ96

Reputation: 313

The search filter cannot be recognized

i am trying to validate users input by using Do-until i want the program to keep looping until a valid input has been given, that is working when the users gives any input regardless of what it is, but when the value is empty "" i get stuck in an endless loop that keeps giving me this error

The user does exist in AD:
samaccountname: 'the username'
Full Name: 'the fullname'  

get-aduser : The search filter cannot be recognized
At line:6 char:9
+ $user = get-aduser -Filter "samaccountname -eq '$SiteOwner' -OR name  -eq '$SiteO ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [Get-ADUser], ADException
+ FullyQualifiedErrorId : ActiveDirectoryServer:8254,Microsoft.ActiveDirectory.Management.Commands.GetADUser

this is my code:

Import-Module ActiveDirectory
Do{

$SiteOwner = Read-Host 'OwnerAlias'

Do {
$user = get-aduser -Filter "samaccountname -eq '$SiteOwner' -OR name -eq '$SiteOwner'"
If (!$user) {
  "The user does not exist in AD"
} 
Else {
"The user does exist in AD:`nsamaccountname: $($user.samaccountname)`nFull Name: $($user.name)"
}



}While (($null -eq $SiteOwner) -or ( "" -eq $SiteOwner))
}Until ($user)

what am i doing wrong?

Upvotes: 1

Views: 7376

Answers (2)

boxdog
boxdog

Reputation: 8442

Here is a version of your code that works:

# Ensure variables are null - may not be needed, depending on the rest of your code (if any)
$user = $null
$SiteOwner = $null

# Assuming $user is null we should do a lookup
while(-not $user) {
    $SiteOwner = Read-Host 'OwnerAlias'

    # As long as the user entered some text, query AD - otherwise, skip this and ask again
    if (-not [string]::IsNullOrWhiteSpace($SiteOwner)) {
        $user = Get-AdUser -Filter "samaccountname -eq '$SiteOwner' -OR name -eq '$SiteOwner'"

        if (-not $user) {
          # Found the user - we should exit the while
          "The user does not exist in AD"
        } 
        else {
          # Didn't find the user - ask for the name again
          "The user does exist in AD:`nsamaccountname: $($user.samaccountname)`nFull Name: $($user.name)"
        }
    }
}

Upvotes: 2

AdminOfThings
AdminOfThings

Reputation: 25021

The problem is that with "" as the input, it is throwing a terminating error. You can account for that with a try-catch block. Inside of that, you can add a break statement to break out of the second do-until loop. This could probably be designed differently though.

Do{

  $SiteOwner = Read-Host 'OwnerAlias'

  Do {
    try {
      $user = get-aduser -Filter "samaccountname -eq '$SiteOwner' -OR name -eq '$SiteOwner'"
    }
    catch {
      "The user does not exist in AD"
      break
    }
    If (!$user) {
      "The user does not exist in AD"
    } 
    Else {
      "The user does exist in AD:`nsamaccountname: $($user.samaccountname)`nFull Name: $($user.name)"
    }

  }While (($null -eq $SiteOwner) -or ( "" -eq $SiteOwner))
}Until ($user)

Upvotes: 3

Related Questions