의 모든 도메인을 통해 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
을 바꿔 모든 하위 도메인에서 검색 할 수 있습니까?