英文:
Problems with Get-ADUser -Filter
问题
我想知道以两个数字结尾的用户有多少个,其中"UserParameters"属性为null。到目前为止,这是我的命令:
```powershell
$var1 = Get-ADUser -Filter {SamAccountName -like "*[0-9][0-9]"} -SearchBase "OU=xxxxxxxxxxxxxx,DC=xxxxxxx,DC=xx" | Where-Object {$_.UserParameters -eq $null}
如果我键入 $var1.count
,它只会回复0。为什么会这样?是因为筛选器(-Filter)的问题,因为如果我只键入 -Filter *
,它就可以正常工作。请帮助我!
<details>
<summary>英文:</summary>
I want to know how many Users whose ending are two numbers there are where the Attribute "UserParameters" is null. This is my command so far:
$var1 = Get-ADUser -Filter {SamAccountName -like "*[0-9][0-9]"} -SearchBase "OU=xxxxxxxxxxxxxx,DC=xxxxxxx,DC=xx" | Where-Object {$_.UserParameters -eq $null}
If I type in ```$var1.count``` it just replies with 0. Why is that so? And yes there are users that end with two numbers. I think the -Filter is the Problem because if I just type ```-Filter *``` it works just fine.
Please help me!
</details>
# 答案1
**得分**: 1
AD筛选器提供程序不知道通配符`[0-9]`的含义。如果需要的话,您可以自己构造一个LDAP筛选器,它本质上是一个OR模式`|`:
```none
(|(samAccountName=*00)(samAccountName=*01)(samAccountName=*02)(samAccountName=*03)...)
例如:
$filter = "(|"
foreach($i in 0..9) {
foreach($x in 0..9) {
$filter += "(samAccountName=*$i$x)"
}
}
$filter += ")"
Get-ADUser -LDAPFilter $filter -SearchBase 'OU=xxxxxxxxxxxxxx,DC=xxxxxxx,DC=xx'
英文:
The AD Filter provider has no clue what the wildcard [0-9]
means. You can construct an LDAP Filter yourself if needed which would essentianlly be an OR pattern |
:
(|(samAccountName=*00)(samAccountName=*01)(samAccountName=*02)(samAccountName=*03)...)
For example:
$filter = "(|"
foreach($i in 0..9) {
foreach($x in 0..9) {
$filter += "(samAccountName=*$i$x)"
}
}
$filter += ")"
Get-ADUser -LDAPFilter $filter -SearchBase 'OU=xxxxxxxxxxxxxx,DC=xxxxxxx,DC=xx'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论