IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

VBScript Discussion :

Lister arborescence OU dans Active Directory


Sujet :

VBScript

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre à l'essai
    Femme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Septembre 2011
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Femme

    Informations professionnelles :
    Activité : Ingénieur systèmes et réseaux

    Informations forums :
    Inscription : Septembre 2011
    Messages : 5
    Par défaut Lister arborescence OU dans Active Directory
    Bonjour,

    Je souhaite parcourir toute une arborescence d'OU dans mon AD et afficher (dans un fichier .txt) la description de ces OU au fur et à mesure que je parcours l'arborescence.
    J'arrive déjà à parcourir mon arborescence et lister mes utilisateurs par OU mais j'aimerais afficher tout le chemin pour ces utilisateurs et pas seulement l'OU qui les contient.
    J'espère avoir été clair. Merci d'avance de votre aide.

    Cordialement,

  2. #2
    Membre chevronné Avatar de pitchalov
    Homme Profil pro
    Inscrit en
    Avril 2007
    Messages
    340
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : France

    Informations forums :
    Inscription : Avril 2007
    Messages : 340
    Par défaut
    Bonjour,

    Pour afficher le chemin complet d'un utilisateur (et d'ailleurs de l'ensemble des objets d'un AD), utilise la propriété "distinguishedName".

    Après si tu as besoin d'aide spécifique à ton script, ça sera difficile de t'apporter de l'aide si tu ne poste pas ton code.

    Bonne continuation.

  3. #3
    Membre à l'essai
    Femme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Septembre 2011
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Femme

    Informations professionnelles :
    Activité : Ingénieur systèmes et réseaux

    Informations forums :
    Inscription : Septembre 2011
    Messages : 5
    Par défaut
    Merci de ta réponse. Effectivement, la propriété distinguishedName permet d'obtenir un chemin complet pour un utilisateur. Seulement, je ne souhaite obtenir le même chemin mais plutôt avec la propriété Description des OU du style DescriptionOuParent1->DescriptionOuEnfant : liste des utilisateurs
    DescriptionOuParent1->DescriptionOuEnfant2 : liste des utilisateurs

    etc...

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    'On Error Resume Next
     
     
    '   Variables 
     
    Const ADS_GROUP_TYPE_BUILTIN = &H1
    Const ADS_GROUP_TYPE_GLOBAL = &H2
    Const ADS_GROUP_TYPE_LOCAL = &H4
    Const ADS_GROUP_TYPE_UNIVERSAL = &H8
    Const ADS_GROUP_TYPE_SECURITY_ENABLED = &H80000000
     
    Const ADS_UF_ACCOUNTDISABLE = 2
     
    Const ForReading = 1 
    Const ForWriting = 2 
    Const ForAppending = 8
     
    dim nb_ou
    dim ou()
    dim ou_lib()
     
     
     
    ' ********************************************************************************
    ' *  Vérification des paramètres 
    ' ********************************************************************************
    Set objArgs = WScript.Arguments
     
     
    Affiuser=1 
    StrOURecherche="OU=test"
    FichierResultats="traceruser.txt" 
     
     ' ********************************************************************************
    ' *  Connexion a l'AD 
    ' ********************************************************************************
     
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objTextFile = objFSO.OpenTextFile(FichierResultats , ForWriting, True)
    Set objRootDSE = GetObject("LDAP://RootDSE")
    strForest = objRootDSE.Get("rootDomainNamingContext")
    Set objForest = GetObject("LDAP://OU=OUParent,OU=MonOu," & strForest )
    Set adoCommand = CreateObject("ADODB.Command")
    Set adoConnection = CreateObject("ADODB.Connection")
    adoConnection.Provider = "ADsDSOObject"
    adoConnection.Open "Active Directory Provider"
    adoCommand.ActiveConnection = adoConnection
    adoCommand.Properties("Page Size") = 100
    adoCommand.Properties("Timeout") = 30
    adoCommand.Properties("Cache Results") = False
    Set objFSO_A = CreateObject("Scripting.FileSystemObject")
    Set objTextFile_A = objFSO_A.OpenTextFile("trace.txt" , ForWriting, True)
     
     
     
    Call EnumDomains(objForest)
     
     
    ' ********************************************************************************
    ' *  Fin du Script + fermeture de la connexion AD 
    ' ********************************************************************************
     
    wscript.echo "FIN DE TRAITEMENT"
     
    adoConnection.Close
     
     
    ' ********************************************************************************
    ' *  Enumeration des OU racine au DOMAINE
    ' ********************************************************************************
    Sub EnumDomains(ByVal objParent)
     
    	objParent.Filter = Array("organizationalUnit")
        For Each objContainer In objParent
     
    		if objContainer.Name=StrOURecherche then
     
    			Call EnumContainers(objContainer,  "")
     
    		end if
    	Next
     
    End Sub
     
    ' ********************************************************************************
    ' *  Enumeration des OU
    ' ********************************************************************************
    Sub EnumContainers(ByVal objParent, ByVal strOffset)
     
    		objParent.Filter = Array("organizationalUnit")
     
         strManagedBy = objparent.managedby
        If IsEmpty(objparent.managedby) = TRUE Then
          xw="(Pas de Manager)"
              Else
          Set objUser = GetObject("LDAP://" & objparent.managedby)
          ipphoneX=find_phone(objuser.distinguishedName)
     
          If len(ipphoneX)<>4 then 
               xw=" Managé par : " & objUser.Get("displayName") & "  (Messagerie : " & objUser.Get("mail") & ") (Matricule : " & objUser.Get("employeeNumber") & ")"
          else
               xw=" Managé par : " & objUser.Get("displayName") &   "  (Messagerie : " & objUser.Get("mail") & ") (Téléphone : " & ipphoneX &") (Matricule : " & objUser.Get("employeeNumber") & ")"
     
         end if
     
        End If
     
     
       objTextFile.WriteLine("")
       objTextFile.WriteLine(strOffset & " " &right(objParent.Name,len(objParent.Name)-3) & " : " & objParent.Description & xw )
    	 	objTextFile_A.WriteLine (objParent.Description)
    	 	objTextFile_A.WriteLine ("Organisation : " &right(objParent.Name,len(objParent.Name)-3) & " " & objParent.distinguishedName )
        objTextFile_A.WriteLine ("")
        objTextFile_A.WriteLine (xw)
        objTextFile_A.WriteLine ("") 
     
    	objTextFile.WriteLine("")	
     
        Call ListeUsers(objParent, ";" & strOffset)
    		objParent.Filter = Array("organizationalUnit")
     
    		For Each objChild In objParent
     
    			Call EnumContainers(objChild,";" & strOffset)
     
    		Next		
     
     
     
    End Sub
     
    ********************************************************************************
    ' *  Listage des Utilisateurs
    ' ********************************************************************************
    Sub ListeUsers(ByVal objContainer, ByVal strOffset)
        objContainer.Filter = Array("user")
        lngUsers = 0
        lngComputers = 0
        i=0
        For Each objMember In objContainer
            If (LCase(objMember.Class) = "user") Then
    			if Affiuser=1 then
     
     
    			 	'--------------------------------------------------'
    			 	'test si compte désactivé ? et date expiration...
    			 	'--------------------------------------------------'
     
    			 	Set bobjConnection = CreateObject("ADODB.Connection")
            bobjConnection.Open "Provider=ADsDSOObject;"
            Set bobjCommand = CreateObject("ADODB.Command")
            bobjCommand.ActiveConnection = bobjConnection
            bobjCommand.CommandText = "<LDAP://" & objMember.distinguishedname & ">;(objectCategory=User);distinguishedName,mail,employeeNumber,userAccountControl;subtree"  
            Set bobjRecordSet = bobjCommand.Execute
     
            intUAC=bobjRecordset.Fields("userAccountControl")
            d_mes=""
            If intUAC AND ADS_UF_ACCOUNTDISABLE Then
                      Set wobjUser = GetObject("LDAP://"& bobjRecordset.Fields("distinguishedName"))
     
                      dtmAccountExpiration = wobjUser.AccountExpirationDate 
     
                      If Err.Number = -2147467259 OR dtmAccountExpiration = #1/1/1970# Then
                      d_mes=" (Compte Désactivé)"
                      Else
                       d_mes= " (Compte Désactivé expiré le : " & wobjUser.AccountExpirationDate &")"
                      End If
     
                   End If
     
     
     
    			 	bobjConnection.Close
     
             if objMember.ipPhone<>"" then 
                objTextFile_A.WriteLine  (right(objMember.Name,len(objMember.Name)-3) & " (Telephone : " & objMember.ipPhone & ") (Messagerie : " &  objMember.mail & ")" & ") (Matricule : " &  objMember.employeeNumber & ")" & d_mes  )
             objTextFile.WriteLine (strOffset & right(objMember.Name,len(objMember.Name)-3) & " (" & objMember.ipPhone & ")" & d_mes  )
     
             else
    			 	    objTextFile_A.WriteLine  (right(objMember.Name,len(objMember.Name)-3) &" (Messagerie : " &  objMember.mail &  ") (Matricule : " &  objMember.employeeNumber & ")" &d_mes )
    			 	    objTextFile.WriteLine (strOffset & right(objMember.Name,len(objMember.Name)-3) &" " &d_mes )
    			 	end if
     
             i=i+1
    			end if
     
    			'objTextFile.WriteLine(strOffset & "" & right(objMember.Name,len(objMember.Name)-3))
     
            End If
        Next
        objTextFile_A.WriteLine ("")
        objTextFile_A.WriteLine (i & " agent(s)")
        'objTextFile.WriteLine(strOffset & "SAUT PAGE")
        objTextFile_A.WriteLine "---------------------------------------------------------------" & chr(10)& chr(13) & chr(10)
    End Sub
     
     
     
    function find_phone (x)
    Set aobjConnection = CreateObject("ADODB.Connection")
    aobjConnection.Open "Provider=ADsDSOObject;"
    Set aobjCommand = CreateObject("ADODB.Command")
    aobjCommand.ActiveConnection = aobjConnection
    aobjCommand.CommandText = "<LDAP://" & x & ">;(objectCategory=User);distinguishedName,ipPhone;subtree"  
    Set aobjRecordSet = aobjCommand.Execute
    find_phone=aobjRecordset.Fields("ipPhone")
    aobjConnection.Close
    end function

    Merci d'avance

  4. #4
    Membre chevronné Avatar de pitchalov
    Homme Profil pro
    Inscrit en
    Avril 2007
    Messages
    340
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : France

    Informations forums :
    Inscription : Avril 2007
    Messages : 340
    Par défaut
    Bonjour,

    D'accord, avec ton code on voit mieux ton besoin, qui n'est au final que de la gestion de chaines de caractères au travers de plusieurs fonctions.

    Vois si cette petite évol t'apporte ce que tu cherche, ou si ça peut t'aider à obtenir le retour que tu veux :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    'On Error Resume Next
     
     
    '   Variables 
     
    Const ADS_GROUP_TYPE_BUILTIN = &H1
    Const ADS_GROUP_TYPE_GLOBAL = &H2
    Const ADS_GROUP_TYPE_LOCAL = &H4
    Const ADS_GROUP_TYPE_UNIVERSAL = &H8
    Const ADS_GROUP_TYPE_SECURITY_ENABLED = &H80000000
     
    Const ADS_UF_ACCOUNTDISABLE = 2
     
    Const ForWriting = 2 
     
     
     
    ' ********************************************************************************
    ' *  Vérification des paramètres 
    ' ********************************************************************************
    Affiuser=1 
    StrOURecherche="OU=Test"
    FichierResultats="traceruser.txt" 
     
     ' ********************************************************************************
    ' *  Connexion a l'AD 
    ' ********************************************************************************
     
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objTextFile = objFSO.OpenTextFile(FichierResultats , ForWriting, True)
    Set objRootDSE = GetObject("LDAP://RootDSE")
    strForest = objRootDSE.Get("rootDomainNamingContext")
    Set objForest = GetObject("LDAP://" & strForest )
    Set adoCommand = CreateObject("ADODB.Command")
    Set adoConnection = CreateObject("ADODB.Connection")
    adoConnection.Provider = "ADsDSOObject"
    adoConnection.Open "Active Directory Provider"
    adoCommand.ActiveConnection = adoConnection
    adoCommand.Properties("Page Size") = 100
    adoCommand.Properties("Timeout") = 30
    adoCommand.Properties("Cache Results") = False
    Set objFSO_A = CreateObject("Scripting.FileSystemObject")
    Set objTextFile_A = objFSO_A.OpenTextFile("trace.txt" , ForWriting, True)
     
     
     
    Call EnumDomains(objForest)
     
     
    ' ********************************************************************************
    ' *  Fin du Script + fermeture de la connexion AD 
    ' ********************************************************************************
     
    wscript.echo "FIN DE TRAITEMENT"
     
    adoConnection.Close
     
     
    ' ********************************************************************************
    ' *  Enumeration des OU racine au DOMAINE
    ' ********************************************************************************
    Sub EnumDomains(ByVal objParent)
     
    	objParent.Filter = Array("organizationalUnit")
        For Each objContainer In objParent
     
    		if objContainer.Name=StrOURecherche then
     
    			Call EnumContainers(objContainer,  "")
     
    		end if
    	Next
     
    End Sub
     
    ' ********************************************************************************
    ' *  Enumeration des OU
    ' ********************************************************************************
    Sub EnumContainers(ByVal objParent, ByVal strOffset)
     
    		objParent.Filter = Array("organizationalUnit")
     
         strManagedBy = objparent.managedby
        If IsEmpty(objparent.managedby) = TRUE Then
          xw="(Pas de Manager)"
              Else
          Set objUser = GetObject("LDAP://" & objparent.managedby)
          ipphoneX=find_phone(objuser.distinguishedName)
     
          If len(ipphoneX)<>4 then 
               xw=" Managé par : " & objUser.Get("displayName") & "  (Messagerie : " & objUser.Get("mail") & ") (Matricule : " & objUser.Get("employeeNumber") & ")"
          else
               xw=" Managé par : " & objUser.Get("displayName") &   "  (Messagerie : " & objUser.Get("mail") & ") (Téléphone : " & ipphoneX &") (Matricule : " & objUser.Get("employeeNumber") & ")"
     
         end if
     
        End If
     
     
       objTextFile.WriteLine("")
       objTextFile.WriteLine(strOffset & " --> OU : " & right(objParent.Name,len(objParent.Name)-3) & " : " & objParent.Description & xw )
     
    	strOffset = strOffset & " --> " & right(objParent.Name,len(objParent.Name)-3)
     
        Call ListeUsers(objParent, vbTab & strOffset)
    		objParent.Filter = Array("organizationalUnit")
     
    		For Each objChild In objParent
     
    			Call EnumContainers(objChild, strOffset)
     
    		Next		
     
     
     
    End Sub
     
    ' ********************************************************************************
    ' *  Listage des Utilisateurs
    ' ********************************************************************************
    Sub ListeUsers(ByVal objContainer, ByVal strOffset)
        objContainer.Filter = Array("user")
        lngUsers = 0
        lngComputers = 0
        i=0
        For Each objMember In objContainer
            If (LCase(objMember.Class) = "user") Then
    			if Affiuser=1 then
     
     
    			 	'--------------------------------------------------'
    			 	'test si compte désactivé ? et date expiration...
    			 	'--------------------------------------------------'
     
    			 	Set bobjConnection = CreateObject("ADODB.Connection")
            bobjConnection.Open "Provider=ADsDSOObject;"
            Set bobjCommand = CreateObject("ADODB.Command")
            bobjCommand.ActiveConnection = bobjConnection
            bobjCommand.CommandText = "<LDAP://" & objMember.distinguishedname & ">;(objectCategory=User);distinguishedName,mail,employeeNumber,userAccountControl;subtree"  
            Set bobjRecordSet = bobjCommand.Execute
     
            intUAC=bobjRecordset.Fields("userAccountControl")
            d_mes=""
            If intUAC AND ADS_UF_ACCOUNTDISABLE Then
                      Set wobjUser = GetObject("LDAP://"& bobjRecordset.Fields("distinguishedName"))
     
                      dtmAccountExpiration = wobjUser.AccountExpirationDate 
     
                      If Err.Number = -2147467259 OR dtmAccountExpiration = #1/1/1970# Then
                      d_mes=" (Compte Désactivé)"
                      Else
                       d_mes= " (Compte Désactivé expiré le : " & wobjUser.AccountExpirationDate &")"
                      End If
     
                   End If
     
     
     
    			 	bobjConnection.Close
     
             if objMember.ipPhone<>"" then 
                objTextFile_A.WriteLine  (right(objMember.Name,len(objMember.Name)-3) & " (Telephone : " & objMember.ipPhone & ") (Messagerie : " &  objMember.mail & ")" & ") (Matricule : " &  objMember.employeeNumber & ")" & d_mes  )
             objTextFile.WriteLine (strOffset & " --> Utilisateur : " & right(objMember.Name,len(objMember.Name)-3) & " (" & objMember.ipPhone & ")" & d_mes  )
     
             else
    			 	    objTextFile_A.WriteLine  (right(objMember.Name,len(objMember.Name)-3) &" (Messagerie : " &  objMember.mail &  ") (Matricule : " &  objMember.employeeNumber & ")" &d_mes )
    			 	    objTextFile.WriteLine (strOffset & " --> Utilisateur : " & right(objMember.Name,len(objMember.Name)-3) &" " &d_mes )
    			 	end if
     
             i=i+1
    			end if
     
    			'objTextFile.WriteLine(strOffset & "" & right(objMember.Name,len(objMember.Name)-3))
     
            End If
        Next
        objTextFile_A.WriteLine ("")
        objTextFile_A.WriteLine (i & " agent(s)")
        'objTextFile.WriteLine(strOffset & "SAUT PAGE")
        objTextFile_A.WriteLine "---------------------------------------------------------------" & chr(10)& chr(13) & chr(10)
    End Sub
     
     
     
    function find_phone (x)
    Set aobjConnection = CreateObject("ADODB.Connection")
    aobjConnection.Open "Provider=ADsDSOObject;"
    Set aobjCommand = CreateObject("ADODB.Command")
    aobjCommand.ActiveConnection = aobjConnection
    aobjCommand.CommandText = "<LDAP://" & x & ">;(objectCategory=User);distinguishedName,ipPhone;subtree"  
    Set aobjRecordSet = aobjCommand.Execute
    find_phone=aobjRecordset.Fields("ipPhone")
    aobjConnection.Close
    end function
    Bonne continuation

  5. #5
    Membre à l'essai
    Femme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Septembre 2011
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Femme

    Informations professionnelles :
    Activité : Ingénieur systèmes et réseaux

    Informations forums :
    Inscription : Septembre 2011
    Messages : 5
    Par défaut
    Merci d'avance de ta réponse. Je te tiens au courant dès que possible. Merci

  6. #6
    Membre à l'essai
    Femme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Septembre 2011
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Femme

    Informations professionnelles :
    Activité : Ingénieur systèmes et réseaux

    Informations forums :
    Inscription : Septembre 2011
    Messages : 5
    Par défaut
    Super ! C'était quasiment ce que je recherchais.

    Voici le code final :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    'On Error Resume Next
     
     
    '   Variables 
     
    Const ADS_GROUP_TYPE_BUILTIN = &H1
    Const ADS_GROUP_TYPE_GLOBAL = &H2
    Const ADS_GROUP_TYPE_LOCAL = &H4
    Const ADS_GROUP_TYPE_UNIVERSAL = &H8
    Const ADS_GROUP_TYPE_SECURITY_ENABLED = &H80000000
     
    Const ADS_UF_ACCOUNTDISABLE = 2
     
    Const ForWriting = 2 
     
     
     
    ' ********************************************************************************
    ' *  Vérification des paramètres 
    ' ********************************************************************************
    Affiuser=1 
    StrOURecherche="OU=test"
    FichierResultats="traceruser.txt" 
     
     ' ********************************************************************************
    ' *  Connexion a l'AD 
    ' ********************************************************************************
     
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objTextFile = objFSO.OpenTextFile(FichierResultats , ForWriting, True)
    Set objRootDSE = GetObject("LDAP://RootDSE")
    strForest = objRootDSE.Get("rootDomainNamingContext")
    Set objForest = GetObject("LDAP://" & strForest )
    Set adoCommand = CreateObject("ADODB.Command")
    Set adoConnection = CreateObject("ADODB.Connection")
    adoConnection.Provider = "ADsDSOObject"
    adoConnection.Open "Active Directory Provider"
    adoCommand.ActiveConnection = adoConnection
    adoCommand.Properties("Page Size") = 100
    adoCommand.Properties("Timeout") = 30
    adoCommand.Properties("Cache Results") = False
    Set objFSO_A = CreateObject("Scripting.FileSystemObject")
    Set objTextFile_A = objFSO_A.OpenTextFile("trace.txt" , ForWriting, True)
     
     
     
    Call EnumDomains(objForest)
     
     
    ' ********************************************************************************
    ' *  Fin du Script + fermeture de la connexion AD 
    ' ********************************************************************************
     
    wscript.echo "FIN DE TRAITEMENT"
     
    adoConnection.Close
     
     
    ' ********************************************************************************
    ' *  Enumeration des OU racine au DOMAINE
    ' ********************************************************************************
    Sub EnumDomains(ByVal objParent)
     
    	objParent.Filter = Array("organizationalUnit")
        For Each objContainer In objParent
     
    		if objContainer.Name=StrOURecherche then
     
    			Call EnumContainers(objContainer,  "")
     
    		end if
    	Next
     
    End Sub
     
    ' ********************************************************************************
    ' *  Enumeration des OU
    ' ********************************************************************************
    Sub EnumContainers(ByVal objParent, ByVal strOffset)
     
    		objParent.Filter = Array("organizationalUnit")
     
         strManagedBy = objparent.managedby
        If IsEmpty(objparent.managedby) = TRUE Then
          xw="(Pas de Manager)"
              Else
          Set objUser = GetObject("LDAP://" & objparent.managedby)
          ipphoneX=find_phone(objuser.distinguishedName)
     
          If len(ipphoneX)<>4 then 
               xw="Managé par : " & objUser.Get("displayName") & "  (Messagerie : " & objUser.Get("mail") & ") (Matricule : " & objUser.Get("employeeNumber") & ")"
          else
               xw="Managé par : " & objUser.Get("displayName") &   "  (Messagerie : " & objUser.Get("mail") & ") (Téléphone : " & ipphoneX &") (Matricule : " & objUser.Get("employeeNumber") & ")"
     
         end if
     
        End If
     
     
    	objTextFile.WriteLine("")
    	objTextFile.WriteLine(strOffset & objParent.Description)
    	objTextFile.WriteLine(xw)
    	strOffset = strOffset & objParent.Description & " - "
     
        Call ListeUsers(objParent, vbTab & strOffset)
    		objParent.Filter = Array("organizationalUnit")
     
    		For Each objChild In objParent
     
    			Call EnumContainers(objChild, strOffset)
     
    		Next		
     
     
     
    End Sub
     
    ' ********************************************************************************
    ' *  Listage des Utilisateurs
    ' ********************************************************************************
    Sub ListeUsers(ByVal objContainer, ByVal strOffset)
        objContainer.Filter = Array("user")
        lngUsers = 0
        lngComputers = 0
        i=0
        For Each objMember In objContainer
            If (LCase(objMember.Class) = "user") Then
    			if Affiuser=1 then
     
     
    			 	'--------------------------------------------------'
    			 	'test si compte désactivé ? et date expiration...
    			 	'--------------------------------------------------'
     
    			 	Set bobjConnection = CreateObject("ADODB.Connection")
            bobjConnection.Open "Provider=ADsDSOObject;"
            Set bobjCommand = CreateObject("ADODB.Command")
            bobjCommand.ActiveConnection = bobjConnection
            bobjCommand.CommandText = "<LDAP://" & objMember.distinguishedname & ">;(objectCategory=User);distinguishedName,mail,employeeNumber,userAccountControl;subtree"  
            Set bobjRecordSet = bobjCommand.Execute
     
            intUAC=bobjRecordset.Fields("userAccountControl")
            d_mes=""
            If intUAC AND ADS_UF_ACCOUNTDISABLE Then
                      Set wobjUser = GetObject("LDAP://"& bobjRecordset.Fields("distinguishedName"))
     
                      dtmAccountExpiration = wobjUser.AccountExpirationDate 
     
                      If Err.Number = -2147467259 OR dtmAccountExpiration = #1/1/1970# Then
                      d_mes=" (Compte Désactivé)"
                      Else
                       d_mes= " (Compte Désactivé expiré le : " & wobjUser.AccountExpirationDate &")"
                      End If
     
                   End If
     
     
     
    			 	bobjConnection.Close
     
             if objMember.ipPhone<>"" then 
                objTextFile_A.WriteLine  (right(objMember.Name,len(objMember.Name)-3) & " (Telephone : " & objMember.ipPhone & ") (Messagerie : " &  objMember.mail & ")" & ") (Matricule : " &  objMember.employeeNumber & ")" & d_mes  )
             objTextFile.WriteLine (right(objMember.Name,len(objMember.Name)-3) & " (" & objMember.ipPhone & ")" & d_mes  )
     
             else
    			 	    objTextFile_A.WriteLine  (right(objMember.Name,len(objMember.Name)-3) &" (Messagerie : " &  objMember.mail &  ") (Matricule : " &  objMember.employeeNumber & ")" &d_mes )
    			 	    objTextFile.WriteLine (right(objMember.Name,len(objMember.Name)-3) &" " &d_mes )
    			 	end if
     
             i=i+1
    			end if
     
    			'objTextFile.WriteLine(strOffset & "" & right(objMember.Name,len(objMember.Name)-3))
     
            End If
        Next
        objTextFile_A.WriteLine ("")
        objTextFile_A.WriteLine (i & " agent(s)")
        'objTextFile.WriteLine(strOffset & "SAUT PAGE")
        objTextFile_A.WriteLine "---------------------------------------------------------------" & chr(10)& chr(13) & chr(10)
    End Sub
     
     
     
    function find_phone (x)
    Set aobjConnection = CreateObject("ADODB.Connection")
    aobjConnection.Open "Provider=ADsDSOObject;"
    Set aobjCommand = CreateObject("ADODB.Command")
    aobjCommand.ActiveConnection = aobjConnection
    aobjCommand.CommandText = "<LDAP://" & x & ">;(objectCategory=User);distinguishedName,ipPhone;subtree"  
    Set aobjRecordSet = aobjCommand.Execute
    find_phone=aobjRecordset.Fields("ipPhone")
    aobjConnection.Close
    end function
    Encore merci et à bientôt

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Lister les utilisateurs dans Active Directory
    Par Thomas Lebrun dans le forum Contribuez
    Réponses: 0
    Dernier message: 29/12/2010, 00h16
  2. Lister les utilisateurs dans Active Directory
    Par Thomas Lebrun dans le forum Contribuez
    Réponses: 0
    Dernier message: 28/12/2010, 22h54
  3. Problème droit dans active directory et sur pc local
    Par krfa1 dans le forum Windows XP
    Réponses: 2
    Dernier message: 17/01/2006, 14h49
  4. Comment lister les objets d'Active Directory ?
    Par fbalien dans le forum API, COM et SDKs
    Réponses: 2
    Dernier message: 16/01/2006, 19h59
  5. [VB.NET] Recherche Info dans Active Directory
    Par Vlat dans le forum Windows Forms
    Réponses: 2
    Dernier message: 30/12/2005, 15h29

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo