2012-08-29 2 views
1

의 모든 도메인을 통해 LDAP 검색은 I가 특정 사용자를 검색하고 Active Directory에서 전체 이름, 이메일, 부서를 출력 다음 VBA 코드 :VBA : 숲

Public Type LDAPUserInfo 
    FullName As String 
    Email As String 
    Department As String 
    AccountStatus As String 
End Type 


Function FindUser(ByVal username) As LDAPUserInfo 
On Error GoTo Err 

Dim objRoot As Variant 
Dim LDAPdomainName As String 
Dim cn As Variant 
Dim cmd As Variant 
Dim rs As Variant 
Dim LDAPUserInfo As LDAPUserInfo 

Set cn = CreateObject("ADODB.Connection") 
Set cmd = CreateObject("ADODB.Command") 
Set rs = CreateObject("ADODB.Recordset") 

Set objRoot = GetObject("LDAP://RootDSE") 
LDAPdomainName = objRoot.Get("defaultNamingContext") 'Contains the distinguished name for the domain of which this directory server is a member. 
'http://msdn.microsoft.com/en-us/library/windows/desktop/ms684291(v=vs.85).aspx 

cn.Open "Provider=ADsDSOObject;" 

cmd.activeconnection = cn 
'cmd.commandtext = "SELECT ADsPath FROM 'LDAP://" & Domain & "' WHERE sAMAccountName = '" & UserName & "'" 
'To see all attributes names available, connect with Active Directory Explorer and add to Select. 
cmd.commandtext = "SELECT cn, mail, physicalDeliveryOfficeName, userAccountControl FROM 'LDAP://" & LDAPdomainName & "' WHERE sAMAccountName = '" & username & "'" 
Set rs = cmd.Execute 

    Debug.Print rs("cn") & " E-mail: " & rs("mail") & " Dept: " & rs("physicalDeliveryOfficeName") 
    LDAPUserInfo.FullName = Nz(rs("cn"), "") 
    LDAPUserInfo.Email = Nz(rs("mail"), "") 
    LDAPUserInfo.Department = Nz(rs("physicalDeliveryOfficeName"), "") 

    FindUser = LDAPUserInfo 


If Not rs Is Nothing Then rs.Close 
If Not cn Is Nothing Then cn.Close 

Exit_Err: 

Set rs = Nothing 
Set cmd = Nothing 
Set cn = Nothing 
Set objRoot = Nothing 
Exit Function 

Err: 

If Err <> 0 Then 
    MsgBox "Error connecting to Active Directory Database: " & Err.Description & vbCrLf & _ 
      "User: " & username, , "Error: " & Err.Number 
Else 
    If Not rs.BOF And Not rs.EOF Then 
     rs.MoveFirst 
     MsgBox rs(0) 
    Else 
     MsgBox "Not Found" 
    End If 
End If 
Resume Exit_Err 

End Function 

그것은 사용자와 함께 작동하는 메인 도메인에 있습니다. LDAPdomainName을 바꿔 모든 하위 도메인에서 검색 할 수 있습니까?

답변

1

답변은 특정 포리스트 구성에 따라 약간 다를 수 있습니다.

일반적으로 어린이 도메인을 검색하려는 경우 ADSI에 추적 조회를 수행하도록 요청할 수 있습니다. ADSI + 조회를 검색하면 조회수가 많아지고 궁극적으로 사용하는 API에 따라 각각에 대한 답변이 있습니다. 여기에 일부 정보 :

네임 스페이스의 관점에서 포리스트에있는 모든 도메인에 대한 하나의 부모가없는 경우에는 도메인이 여러 개인 경우
  • (예 : http://technet.microsoft.com/en-us/library/cc978014.aspx 말했다

    , 약간의 미묘한 차이가 있습니다 : foo.com, bar.foo.com 및 blech.com이있는 포리 스트를 상상해보십시오 ... 모두 다 커버하는 단일 부모는 없습니다) 그런 다음 여러 검색을 수행하거나 팬텀 루트 컨트롤이라고 불리는 것을 사용해야합니다. AD를 지시하여 부모님을 검색하도록 지시하는 부모가 아닌 부모를 전달할 수 있습니다.)

  • 이 검색은 전 세계의 DC를 공격합니다 st ... 당신이 쫓아가는 각 도메인마다 하나씩. 제한된 속성 집합 만 검색하는 경우 대신 글로벌 카탈로그 서버를 사용하여 해당 서버의 모든 도메인에 대한 정보를 제공 할 수 있습니다 (즉, 모두 로컬 인 것처럼 훨씬 빠른 검색). 이렇게하려면 일반적으로 3268/3269 인 글로벌 카탈로그 포트 (후자는 LDAPS)에 연결해야합니다.