Vba tester existence utilisateur dans AD

Bonjour,
Je souhaite lorsqu'on entre un identifiant dans un textbox, qu'une macro fasse la verification dans active directory.
Si l'identifiant n'existe pas, alors un message box apparaît, comment faire?

Merci !

7 réponses

  1. Merci de m'aider c'est assez urgent

    Private Sub textbox1_afterupdate()
    
    Dim conn As New ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim oRoot As IADs
    Dim oDomain As IADs
    Dim sBase As String
    Dim sFilter As String
    Dim sDomain As String
    
    Dim sAttribs As String
    Dim sDepth As String
    Dim sQuery As String
    Dim sAns As String
    
    Dim user As IADsUser
    
    On Error GoTo ErrHandler:
    
    
    
    Set oRoot = GetObject("LDAP://rootDSE")
    'work in the default domain
    sDomain = oRoot.Get("defaultNamingContext")
    Set oDomain = GetObject("LDAP://" & sDomain)
    sBase = "<" & oDomain.ADsPath & ">"
    
    sFilter = "(&(objectCategory=person)(objectClass=user)(name=" _
      & LoginName & "))"
    sAttribs = "adsPath"
    sDepth = "subTree"
    
    sQuery = sBase & ";" & sFilter & ";" & sAttribs & ";" & sDepth
                       
    conn.Open _
      "Data Source=Active Directory Provider;Provider=ADsDSOObject"
      
    Set rs = conn.Execute(sQuery)
    
    If Not rs.EOF Then
        Set user = GetObject(rs("adsPath"))
        With user
        
        
        sAns = "First Name: " & .FirstName & vbCrLf
        sAns = sAns & "Last Name " & .LastName & vbCrLf
        sAns = sAns & "Employee ID: " & .EmployeeID & vbCrLf
        sAns = sAns & "Title: " & .Title & vbCrLf
        sAns = sAns & "Division: " & .Division & vbCrLf
        sAns = sAns & "Department: " & .Department & vbCrLf
    
           
        End With
    End If
    UserInfo = sAns
    ErrHandler:
    
    On Error Resume Next
    If Not rs Is Nothing Then
        If rs.State <> 0 Then rs.Close
        Set rs = Nothing
    End If
    
    If Not conn Is Nothing Then
        If conn.State <> 0 Then conn.Close
        Set conn = Nothing
    End If
    TextBox2.Value = UserInfo 'ou msgbox UserInfo 
    Set oRoot = Nothing
    Set oDomain = Nothing
    
    End If
    End Sub


    Toutes mes références sont activées, et je n'ai aucune information remplissant le textbox2.
    comment faire?
    0
    1. Contributeur
      bonjour,

      ce Vbscript à aménager pourrait peut-être t'aider

      strUserName = "kenmyer"
      dtStart = TimeValue(Now())
      Set objConnection = CreateObject("ADODB.Connection")
      objConnection.Open "Provider=ADsDSOObject;"
       
      Set objCommand = CreateObject("ADODB.Command")
      objCommand.ActiveConnection = objConnection
       
      objCommand.CommandText = _
          ";(&(objectCategory=User)" & _
               "(samAccountName=" & strUserName & "));samAccountName;subtree"
        
      Set objRecordSet = objCommand.Execute
       
      If objRecordset.RecordCount = 0 Then
          WScript.Echo "sAMAccountName: " & strUserName & " does not exist."
      Else
          WScript.Echo strUserName & " exists."
      End If
       
      objConnection.Close


      source:
      http://www.activexperts.com/activmonitor/windowsmanagement/scripts/activedirectory/user/retrieving/

      Etant sur monoposte, je ne peux ni tester, ni t'aider plus, désolé (recordset.recordcount m'inquiète un peu...)
      0
      1. En effet il y a une erreur sur :

        Set objRecordSet = objCommand.Execute

        If objRecordset.RecordCount = 0 Then

        Je ne sais pas comment faire.
        Merci
        0
        1. Contributeur
          pour ma part j'aurais écrit:

          CommandText =";(&(objectCategory=User)" & _"(samAccountName="strUserName&"));samAccountName;subtree"

          Set objRecordSet = objCommand.Execute(commandtext)

          if not objrecorset.eof then
          msgbox struser & " est répertorié"
          else
          msgbox struser & "n'est pas repertorié"
          end if

          mais...
          0
          1. Il y a une erreur sur :

            CommandText =";(&(objectCategory=User)" & _"(samAccountName="strUserName&"));samAccountName;subtree"

            J'aimerai bien avoir un code fonctionnel !
            J'ai un textbox1, j'entre l'identifiant d'un user présent dans active directory, si l'utilisateur n'existe pas alors MsgBox.
            0
            1. Contributeur
              Excuses moi de t'avoir dérangé
              0
              1. Merci quand même de m'avoir aider ! mais je suis novice et je ne comprend pa ton code!
                0