我有一个搜索AD以查找各个组成员的函数。如果我删除循环,我不会收到错误“名为'cn'的列已经属于此数据表”,但是,我需要遍历每个OU。
Function getCOMDLNames(ByVal searchStr As String) As DataTable
Dim MySearchRoot As DirectoryEntry = New DirectoryEntry("path", "usr", "pwd")
Dim MyDirectorySearcher As New DirectorySearcher(MySearchRoot)
Dim con As New SqlConnection
Dim cmdSelect As SqlCommand
Dim da As New SqlDataAdapter
Dim cdt As New DataTable
Dim row As DataRow
Dim campusGroupArray() As String
Dim j As Integer = 0
Try
con.ConnectionString = ConfigurationManager.AppSettings("sql_dir")
con.Open()
cmdSelect = New SqlCommand("...")
cmdSelect.Connection = con
da.SelectCommand = cmdSelect
da.Fill(cdt)
ReDim campusGroupArray(0 To cdt.Rows.Count)
'Fill array
For i As Integer = 0 To cdt.Rows.Count - 1
row = cdt.Rows(i)
campusGroupArray(i) = row(0)
Next
'Remove empty elements from array
Dim tempStr As String = String.Join("'", campusGroupArray)
campusGroupArray = Nothing
campusGroupArray = tempStr.Split(New Char() {"'"c}, StringSplitOptions.RemoveEmptyEntries)
Dim MySearchResult As SearchResultCollection
'Loop through OU's until members are found
Do
MyDirectorySearcher.Filter = ("(&(objectCategory=person)(memberof=cn=" & searchStr & ", ou=groups, ou=" & campusGroupArray(j) & ", dc=.., dc=.., dc=.., dc=..))")
MyDirectorySearcher.SearchScope = SearchScope.Subtree
MyDirectorySearcher.PropertiesToLoad.Add("cn")
MyDirectorySearcher.Sort.Direction = System.DirectoryServices.SortDirection.Ascending
MyDirectorySearcher.Sort.PropertyName = "cn"
MySearchResult = MyDirectorySearcher.FindAll()
j += 1
Loop Until MySearchResult.Count > 0 Or j > campusGroupArray.Length - 1
Dim myTable As New DataTable
Dim colName As String
'Create columns in table
For Each colName In MyDirectorySearcher.PropertiesToLoad
myTable.Columns.Add(colName, GetType(System.String))
Next
Dim result As SearchResult
'Fill table
For Each result In MySearchResult
Dim dr As DataRow = myTable.NewRow()
For Each colName In MyDirectorySearcher.PropertiesToLoad
If result.Properties.Contains(colName) Then
dr(colName) = CStr(result.Properties(colName)(0))
Else
dr(colName) = ""
End If
Next
myTable.Rows.Add(dr)
Next
Return myTable
Catch ex As Exception
Return Nothing
End Try
End Function
答案 0 :(得分:0)
您可以循环遍历任意数量的OU - 但是您需要在循环开始之前定义要加载的属性列表(.PropertiesToLoad
属性)一次(这确实适用)对于目录搜索器调用之间不会改变的所有属性 - 如搜索范围,排序方向等 - 为每次迭代设置它们绝对没有意义 - 设置它们一次并完成它。
' define the list of properties to load for each search result ONCE before the loop
MyDirectorySearcher.PropertiesToLoad.Add("cn")
MyDirectorySearcher.SearchScope = SearchScope.Subtree
MyDirectorySearcher.Sort.Direction = SortDirection.Ascending
MyDirectorySearcher.Sort.PropertyName = "cn"
' *THEN* Loop through OU's until members are found
Do
MyDirectorySearcher.Filter = ("(&(objectCategory=person)(memberof=cn=" & searchStr & ", ou=groups, ou=" & campusGroupArray(j) & ", dc=.., dc=.., dc=.., dc=..))")
MySearchResult = MyDirectorySearcher.FindAll()
j += 1
Loop Until MySearchResult.Count > 0 Or j > campusGroupArray.Length - 1
PropertiesToLoad
只定义搜索结果将包含的属性列表 - 这是每个搜索的相同列表,并且只能包含属性名称(例如cn
)一次。