我正在Visual Basic 2015中编写一个程序,我正在尝试编写一个程序,该程序将找到用户所属的所有AD组并将其显示在列表框中。我将包括之后将其导出到.csv文件的选项。我可以通过sAMAccountName或DN进行搜索,因为我已经定义了两个变量。我很难过,所有我得到的都是空搜索结果。任何帮助将不胜感激。
Imports System.DirectoryServices
Imports System.DirectoryServices.AccountManagement
Public Class UserMembershipWin
Private Sub queryAD_Click(sender As Object, e As RoutedEventArgs) Handles queryAD.Click
Dim username As String
username = usernameBox.Text
Dim emptybox
emptybox = usernameBox.Text.Length
If emptybox = 0 Then
errormsg.Content = "*Required field"
End If
If emptybox > 0 Then
Dim domain = New PrincipalContext(ContextType.Domain)
Dim user = UserPrincipal.FindByIdentity(domain, username)
Dim userDN
userDN = user.DistinguishedName
Dim ADEntry As New DirectoryEntry("LDAP://DC=domain,DC=com")
Dim Groups As New Collection
Dim mySearcher As DirectorySearcher = New DirectorySearcher(ADEntry)
Dim arrList As New ArrayList()
mySearcher.Filter = "(&(ObjectClass=User)(DN=" & userDN & "))"
mySearcher.PropertiesToLoad.Add("MemberOf")
Dim searchResults As SearchResultCollection = mySearcher.FindAll()
If searchResults.Count = 0 Then
errormsg.Content = "Invalid username"
End If
If searchResults.Count > 0 Then
Dim group As New DirectoryEntry(searchResults(0).Path)
For Each member As Object In group.Properties("MemberOf")
userlistbox.Items.Add(member)
Next
End If
End If
End Sub
End Class
答案 0 :(得分:0)
我已经复制了您的问题并发现通过更改以下行,搜索者返回了值:
mySearcher.Filter = "(&(ObjectClass=User)(distinguishedName=" & userDN & "))"
我不确定sAMAccountName的问题,因为您没有包含示例。