0

我正在使用此代码在活动目录中搜索用户帐户,效果很好。

Dim insPrincipalContext As New PrincipalContext(ContextType.Domain, Environment.UserDomainName, "DC=domain,DC=com")

    Dim insUserPrincipal As New UserPrincipal(insPrincipalContext)
    insUserPrincipal.Description = empID
    Dim insPrincipalSearcher As New PrincipalSearcher()
    Dim currentADUser As System.DirectoryServices.AccountManagement.UserPrincipal
    insPrincipalSearcher.QueryFilter = insUserPrincipal
    Dim results As PrincipalSearchResult(Of Principal) = insPrincipalSearcher.FindAll
    For Each p As Principal In results
        currentADUser = p
        adUsername = currentADUser.SamAccountName
        adLast = currentADUser.Surname
        adFirst = currentADUser.GivenName
        adMail = currentADUser.EmailAddress
    Next

问题是我想要的用户帐户可能位于多个位置,因此我不想指定要搜索的 OU,但它可能位于我不希望从中返回它的 2 个 OU 中。有没有办法从我的搜索中排除这 2 个 OU?

我发现了一些类似的问题,但没有一个以这种方式使用 VB.Net 和 system.directory.accountmanagement 命名空间。

4

2 回答 2

0
Dim reduce = results.Where(Function(p) p.SamAccounNams = "Name").FirstOrDefault

好的,那就这样做吧

Dim reduce results.Where(Function(p) Not p.DistinguishedName.Contains("OU=UnwantedOU1") and Not p.DistinguishedName.Contains("OU=UnwantedOU").ToList()

这将为您提供不在两个不需要的 OU 中的所有用户。

祝你好运

于 2013-09-13T22:32:25.410 回答
0

我想出了如何通过将 DistinguishedName 添加到我的结果中并检查它是否有我想要排除的 OU 来做我想做的事情。这是完整的工作示例。

Dim insPrincipalContext As New PrincipalContext(ContextType.Domain, Environment.UserDomainName, "DC=Mydomain,DC=com")

    Dim insUserPrincipal As New UserPrincipal(insPrincipalContext)
    insUserPrincipal.Description = empID
    Dim insPrincipalSearcher As New PrincipalSearcher()
    Dim currentADUser As System.DirectoryServices.AccountManagement.UserPrincipal
    insPrincipalSearcher.QueryFilter = insUserPrincipal
    Dim results As PrincipalSearchResult(Of Principal) = insPrincipalSearcher.FindAll
    For Each p As Principal In results
        currentADUser = p
        adDistName = currentADUser.DistinguishedName
        If InStr(adDistName, "OU=UnwantedOU1") Or InStr(adDistName, "OU=UnwantedOU2") Then
            Continue For
        Else
            adUsername = currentADUser.SamAccountName
            adLast = currentADUser.Surname
            adFirst = currentADUser.GivenName
            adMail = currentADUser.EmailAddress
            Exit For
        End If
    Next
于 2013-09-16T16:16:56.770 回答