Code VBA a coller dans une macro excel, lancement avec un bouton situé en ligne 1
Ligne de titre en 2, extraction commence en ligne 3
Remplacer
ControleurDomaine... avec tes paramètres.
Private Sub Cde_Recup_Click()
Dim strLDAP, sDomain, oRoot, objConnection, objCommand, objRecordSet, strUserName, accountcontrol, Ligne
Dim intLLTS, intReqCompare, objLogon, strWeeks, strDays, intLogonTime
Const ADS_ACEFLAG_INHERIT_ACE = 2
Const ADS_RIGHT_DS_CREATE_CHILD = 1
Const ADS_ACETYPE_ACCESS_ALLOWED = 0
Const ADS_ACETYPE_ACCESS_DENIED = 1
Const ADS_ACETYPE_SYSTEM_AUDIT = 2
Const ADS_ACETYPE_ACCESS_ALLOWED_OBJECT = 5
Const ADS_ACETYPE_ACCESS_DENIED_OBJECT = 6
Const ADS_ACETYPE_SYSTEM_AUDIT_OBJECT = 7
Const ADS_ACETYPE_SYSTEM_ALARM_OBJECT = 8
Const ADS_SCOPE_SUBTREE = 2
Set oRoot = GetObject("LDAP://rootDSE")
sDomain = oRoot.Get("defaultNamingContext")
strLDAP = "LDAP://" & sDomain
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.Properties("Page Size") = 10000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
Range("A3:BZ8000").ClearContents
Range("A3:BZ8000").Interior.ColorIndex = xlNone
Application.ScreenUpdating = False
Ligne = 3
objCommand.CommandText = "SELECT distinguishedName, lastLogonTimeStamp, TelephoneNumber, Description, Mailnickname, CN, GivenName, SN, DisplayName, Initials, userAccountControl, SamAccountName FROM 'LDAP://ControleurDomaine/OU=Utilisateurs,OU=TonOU,DC=TonDomaine,DC=FR' WHERE objectCategory='User'AND objectClass='user'"
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
On Error Resume Next
Do Until objRecordSet.EOF
Cells(Ligne, 1) = objRecordSet.Fields("SN").Value 'NOM
Cells(Ligne, 2) = objRecordSet.Fields("GivenName").Value 'Prénom
Cells(Ligne, 3) = objRecordSet.Fields("SamAccountName").Value
Cells(Ligne, 4) = objRecordSet.Fields("CN").Value 'CN
Cells(Ligne, 5) = objRecordSet.Fields("Initials").Value 'Initiales
Cells(Ligne, 6) = objRecordSet.Fields("DisplayName").Value 'Nom affiché
Cells(Ligne, 7) = objRecordSet.Fields("Mailnickname").Value
Cells(Ligne, 8) = objRecordSet.Fields("Description").Value
Cells(Ligne, 9) = objRecordSet.Fields("TelephoneNumber").Value
dtmAccountExpiration = objUser.AccountExpirationDate
If Err.Number = -2147467259 Or dtmAccountExpiration = #1/1/1970# Then
Cells(Ligne, 10) = "N/A"
Else
Cells(Ligne, 10) = objUser.AccountExpirationDate
End If
Set objLastLogon = objRecordSet.Fields("LastLogonTimeStamp").Value
intLastLogonTime = objLastLogon.HighPart * (2 ^ 32) + objLastLogon.LowPart
intLastLogonTime = intLastLogonTime / (60 * 10000000)
intLastLogonTime = intLastLogonTime / 1440
Cells(Ligne, 11) = intLastLogonTime + #1/1/1601#
Cells(Ligne, 13) = objRecordSet.Fields("distinguishedName").Value 'DN
accountcontrol = objRecordSet.Fields("userAccountControl").Value
If accountcontrol And 2 Then
Cells(Ligne, 1).EntireRow.Interior.ColorIndex = 17
Cells(Ligne, 12) = "D"
Cells(Ligne, 11) = ""
End If
objRecordSet.MoveNext
Ligne = Ligne + 1
Loop
objConnection.Close
Cells.EntireColumn.AutoFit
Cells.EntireRow.AutoFit
Range("A2:CZ5000").Select
Selection.Sort Key1:=Range("A3"), Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Range("A2").Select
Range("A1").RowHeight = 36
Range("A2").RowHeight = 30
Application.ScreenUpdating = True
MsgBox "Extraction terminée"
End Sub
Yop