简体   繁体   English

Get-ADUser -filter解析

[英]Get-ADUser -filter parsing

I am running a script which takes a person's first and last name from an SAP extract, and reads AD to get their UPN. 我正在运行一个脚本,该脚本从SAP摘录中提取一个人的名字和姓氏,然后读取AD以获取其UPN。 For most people, this works; 对于大多数人来说,这是可行的; however there are a bunch of users whose first name is an issue. 但是,有一堆用户的名字是个问题。 For instance "Philip Davies" (names changed to protect the innocent) in SAP is "Phil Davies" in AD. 例如,SAP中的“ Philip Davies”(名称更改为保护无辜者)在AD中为“ Phil Davies”。 So: I have used the following command and it works: 所以:我已经使用以下命令,它可以工作:

Code: 码:

Get-ADUser -SearchBase "OU=CBC Users,DC=cbc,DC=int" -Filter {GivenName -eq "Phil" -and Surname -eq "Davies"}

I then realised I can check for the first three characters which will NORMALLY be the same in the contracted name... so I did this which also works: 然后我意识到我可以检查前三个字符,它们在合同约定的名称中通常是相同的...因此,我这样做也是可行的:

Code: 码:

Get-ADUser -SearchBase "OU=CBC Users,DC=cbc,DC=int" -Filter {GivenName -like "Phi*" -and Surname -eq "Davies"}

Next step: variables; 下一步:变量; so I try this and it works: 所以我尝试这个,它的工作原理:

Code: 码:

$fna="Phil"
Get-ADUser -SearchBase "OU=CBC Users,DC=cbc,DC=int" -Filter {GivenName -eq $fna -and Surname -eq "Davies"}

But if I try this: 但是,如果我尝试这样做:

Code: 码:

$fna="Philip"
$fna=$fna.Substring(0,3)
Get-ADUser -SearchBase "OU=CBC Users,DC=cbc,DC=int" -Filter {GivenName -like $fna* -and Surname -eq "Davies"}

I get no result. 我没有结果。 It doesn't matter if I use brackets, double-quotes, single-quotes, anything. 是否使用方括号,双引号,单引号等等都没关系。 As soon as I try to parse a variable AND use a wildcard, it either produces an error message or no result. 一旦我尝试解析变量并使用通配符,它​​就会产生错误消息或没有结果。

Can anyone please help me with this either by using the "-ldapfilter" method or telling me how to parse AND wildcard? 谁能使用“ -ldapfilter”方法或告诉我如何解析和通配符来帮助我?

Thanks 谢谢

You should not use the wildcard with a variable since you wish to check it with a name which is a string. 您不应该将通配符与变量一起使用,因为您希望使用一个字符串名称来检查通配符。 So what you can do is directly wrap the string with the wildcard and store the final thing in a variable like: 因此,您可以做的是直接用通配符将字符串包装起来,并将最后的内容存储在变量中,例如:

$fna="Philip"
$fna="$($fna.Substring(0,3))*"

Get-ADUser -SearchBase "OU=CBC Users,DC=cbc,DC=int" -Filter {GivenName -like $fna -and Surname -eq "Davies"}

or you can use the LDAP Filter directly like : 或者您可以直接使用LDAP过滤器,例如:

Get-ADUser -SearchBase "OU=CBC Users,DC=cbc,DC=int" -LDAPFilter "(&(GivenName=$fna)(Sn=Davies))"

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM