2015-12-01 89 views
0

我在Visual Basic 2015中編寫了一個程序,我試圖編寫一個程序來查找用戶所屬的所有AD組並顯示它們一個列表框。之後,我將包括將其導出到.csv文件的選項。我可以通過sAMAccountName或DN進行搜索,因爲我定義了兩個變量。我被困住了,我得到的只是空的搜索結果。任何幫助將不勝感激。查找vb.net用戶的組羣成員

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

我已經複製您的問題,並發現,通過改變以下線的搜索返回的值:如您還沒有一個例子

mySearcher.Filter = "(&(ObjectClass=User)(distinguishedName=" & userDN & "))" 

我不知道有關與sAMAccountName賦的問題。

+0

謝謝!簡單的錯誤,現在一切正常。但是,我得到的是羣組的DN,而不是CN。有什麼方法可以顯示組的CN嗎? – stewardson

+0

我相信你必須添加你想要檢索的屬性給搜索者。 –