0

Here is my code: I try to build a filter string in a function and us it in and get-adobject command, but I get an syntax error an position 1

function build-filter ([string]$searchName)
{
$searchName = '"' + $searchName + '"'
$searchName = "{name -like " + $searchName + "}"
return [string]$searchName
}

$searchname = "user1"

$filter = build-filter $searchname

Get-ADObject -Filter $filter

This is the Error message, unfortunatelly in German

Get-ADObject : Fehler beim Analysieren der Abfrage: "{name -like "user1"}" Fehlermeldung: "syntax error" an folgender Position: "1". In Zeile:12 Zeichen:1 + Get-ADObject -Filter $filter + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ParserError: (:) [Get-ADObject], ADFilterParsingException + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADFilterParsingEx ception,Microsoft.ActiveDirectory.Management.Commands.GetADObject

what am I doing wrong here? do I missunderstand some concept?

3
  • It may help if you translate the error message. Even though I studied German at school, it's far from enough to understand the above. Commented May 14, 2013 at 14:23
  • Try this $searchName = "{name -like $searchName }" Commented May 14, 2013 at 14:31
  • unfortunatelly this didnt work. but leaving out the curly bracets helps as suggested by C.B. thanks anyway Commented May 15, 2013 at 11:40

1 Answer 1

1

try changing this:

$searchName = "{name -like " + $searchName + "}"

in

$searchName = "name -like $searchName"
Sign up to request clarification or add additional context in comments.

1 Comment

the seccond suggestion works: $searchName = "name -like $searchName" I guess the "{" is the one producing the error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.