0

这是我的代码:我尝试在一个函数中构建一个过滤器字符串,然后在 get-adobject 命令中使用它,但是在位置 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

这是错误消息,不幸的是德语

Get-ADObject : Fehler beim Analysieren der Abfrage: "{name -like "user1"}" Fehlermeldung: "syntax error" an folgender Position: "1"。在 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

我在这里做错了什么?我误解了一些概念吗?

4

1 回答 1

1

尝试改变这个:

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

$searchName = "name -like $searchName"
于 2013-05-14T14:29:50.473 回答