# One way to get a List of OUs
Get-ADOrganizationalUnit -Filter * -Properties CanonicalName |
Select-Object -Property CanonicalName
则可以使用Where-Object -FilterScript {}按OU进行筛选
# example, saving on the temp folder
Get-ADUser -f * |
Where-Object -FilterScript {
($_.DistinguishedName -match "CN=\w*,OU=DisabledUsers,DC=<domain>,DC=<DC>" -or
$_.DistinguishedName -match "CN=\w*,OU=Users,DC=<domain>,DC=<DC>") -and
$_.GivenName -ne $null #remove users without givenname, like test users
} |
select @{n="Fullname";e={$_.GivenName+" "+$_.Surname}},
GivenName,Surname,SamAccountName |
Export-CSV -Path "$env:TEMP\all_Users.csv" -NoTypeInformation
# you can open the file to inspect
Invoke-Item "$env:TEMP\all_Users.csv"
# import
$allusers = Import-Csv "$env:TEMP\all_Users.csv"
$allusers.Count # number of lines
用途:
get_score "Jane Done" $allusers.fullname 15 # return the 15 first
get_score "jdoe" $allusers.samaccountname 15
5条答案
按热度按时间jutyujz01#
您可以计算两个字符串之间的Levenshtein distance,并确保它低于某个阈值(可能是1或2)。这里有一个PowerShell的例子:Levenshtein distance in powershell
示例:
hfwmuf9z2#
有趣的问题和答案。但一个可能的更简单的解决方案是通过多个属性进行搜索,因为我希望大多数人都能正确拼写他们的名字:)
ax6ht2ek3#
Soundex算法就是为这种情况而设计的。以下是一些PowerShell代码,可能会有所帮助:
Get-Soundex.ps1
pb3s4cty4#
好的,基于我得到的很好的答案(谢谢@boxdog和@Palle Due),我发布了一个更完整的答案。
主要来源:https://github.com/gravejester/Communary.PASM- PowerShell近似字符串匹配。这个主题的伟大模块。
1)FuzzyMatchScore函数
来源:https://github.com/gravejester/Communary.PASM/tree/master/Functions
简单测试:
创建评分函数:
示例
在大数据集上测试性能
测试
这些时间是相当疯狂的,如果有人明白为什么请评论它。
2)从Active Directory生成一个Name表
最好的方法取决于AD的组织。这里我们有许多OU,但是普通用户将在Users和DisabledUsers中。域和DC也会不同(我在这里将我们的更改为
<domain>
和<DC>
)。则可以使用
Where-Object -FilterScript {}
按OU进行筛选用途:
toe950275#
这在一定程度上适用于各种属性的模糊名称解析,但不适用于“Jacck”拼写错误。我得到了五个结果。