vb .net搜索AD并通过过滤employeeID属性获取samaccountname

时间:2017-07-12 14:13:17

标签: vb.net active-directory

我似乎无法使用此代码

加载对员工ID的任何操作
Private Sub AAA(badge As String)
    Console.WriteLine("AD search for " & badge)
    Dim rootEntry As New DirectoryEntry("GC://dc=contoso,dc=com", "username", "password")
    Dim searcher As New DirectorySearcher(rootEntry)
    searcher.Filter = "(&(objectCategory=person)(employeeid='" + badge + "'))"
    searcher.PropertiesToLoad.Add("EmployeeID")
    searcher.PropertiesToLoad.Add("cn")
    searcher.PropertiesToLoad.Add("sAMAccountName")

    Dim results As SearchResultCollection = searcher.FindAll()
    Console.WriteLine("Results: " & results.Count)
    For Each result As SearchResult In results
        Console.WriteLine(result.Properties("sAMAccountName")(0))
        Console.WriteLine(result.Properties("cn")(0))
        Console.WriteLine(result.Properties("employeeID")(0))
        'list fields
        For i As Integer = 0 To result.Properties.Count
            Console.WriteLine(result.Properties.PropertyNames(i))
        Next
    Next
End Sub

我尝试从过滤器中删除(employeeid=' + badge +')并列出所有用户以显示每个用户的员工ID并通过它们进行maby循环但没有运气

此代码返回samaccountname,cn,adspath(我没有要求),但没有employeeid

有一个用户具有员工ID值,我可以使用以下powershell命令获取用户

Get-ADUser -LDAPFilter "(employeeID=*)"

有人能看到错误吗?

2 个答案:

答案 0 :(得分:0)

我记得,几年前,我为广告编写了一个简单的AD查询和导出工具,它还显示了LDAP字段的完整列表,当我刚才运行它时,我在列表中看到了EmployeeID字段。 这是我用来解决(当前)问题的代码。

    Dim searcher As DirectorySearcher = New DirectorySearcher(New DirectoryEntry("LDAP://" & Environment.UserDomainName), "(&(objectCategory=person)(employeeid=" + badge + "))")
    searcher.PropertiesToLoad.Add("EmployeeID")
    searcher.PropertiesToLoad.Add("cn")
    searcher.PropertiesToLoad.Add("sAMAccountName")
    Dim foundUser As SearchResultCollection = searcher.FindAll
    For Each result As SearchResult In foundUser
        Console.WriteLine(result.Properties("sAMAccountName")(0))
        Console.WriteLine(result.Properties("cn")(0))
        Console.WriteLine(result.Properties("employeeID")(0))
    Next

我不知道为什么,但在初始化时通过过滤器可以正常工作。 我也没有将用户名和密码传递给DirectoryEntry对象(即使我使用的用户位于域管理员组

答案 1 :(得分:0)

如何在VB.net目录searcher.filter选项中使用下面的PowerShell代码

Get-ADUser -Filter {Enabled -eq "True"}