2016-09-26 80 views
0

我可以很容易地从我们的域的AD获取OU的列表,但我只想用用户帐户来获取OU。仅显示包含用户帐户的Active Directory OU

我目前正在抓取OU的列表,然后通过每个列表获取用户列表和计数,但处理所有这些数据可能需要大约30秒。

我希望有一些更快的方法来完成相同的任务。

Public Sub GetActiveDirectoryOuList() 
    Dim de As DirectoryEntry = Nothing 
    Dim ds As DirectorySearcher = Nothing 
    Dim results As SearchResultCollection = Nothing 

    Try 
     de = New DirectoryEntry("LDAP://DC=csileasing,DC=com") 
     ds = New DirectorySearcher(de, "(objectClass=organizationalUnit)") 
     results = ds.FindAll 

     If results.Count = 0 Then Exit Try 

     For Each result As SearchResult In results 
      Dim count As Integer = GetUserCountForOU(result.Properties("distinguishedName")(0).ToString) 
      If count > 0 Then 
       Dim ou As New OrgUnitInfo 
       ou.DistinguisedName = result.Properties("distinguishedName")(0).ToString 
       ou.Name = result.Properties("name")(0).ToString 
       ou.UsersCount = count 
       adOrgUnitList.Add(ou) 
      End If 
     Next 

    Catch ex As Exception 
     'MessageBox.Show(ex.Message) 
    Finally 
     If ds IsNot Nothing Then ds.Dispose() 
     If de IsNot Nothing Then de.Dispose() 
     If results IsNot Nothing Then results.Dispose() 
    End Try 

End Sub 
+0

您可以在某处缓存结果,并根据特定的计划或基于特定请求使缓存无效,从而使缓存失效。然后,当您需要获取信息时,请从缓存中读取。 –

+0

是否有一种方法可以在不读取完整用户列表的情况下获取用户数量,然后进行计数? 从AD的每个OU中读取所有用户是永久性的部分。 – JoshF

回答

0

我会确保我的DirectorySearcher操作要求尽可能少的数据。在你的搜索OU只指定你想使用的参数,因为这样

ds = New DirectorySearcher(de, "(objectClass=organizationalUnit)", {"distinguishedName", "name"}) 

也可能试图为您GetUserCountForOU方法做同样的,如果是搜索,然后用自己的所有属性的所有用户来说,这是很正常的,需要很长时间。您可以指定一个空的PropertiesToLoad阵列,其中Filter="(&(objectClass=user)(!objectClass=computer))"DirectorySearcher只会查找objectGuid(如果我正确记得这一点)。您只需计算结果条目即可获得您的ou.UsersCount

+0

感谢您的建议Masma。当我回到这个项目时,我会尝试一下。 – JoshF