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
| Option Explicit
'déclaration des variables
Dim fso
Dim fichier
Dim objDictionary
Const ForReading = 1
Dim i
Dim valeur
Dim tableau
Dim Ligne
Dim nom
Dim conteneur
Dim Root
Dim DomainPath
Dim objConnection
Dim objCommand
Dim objRecordSet
Dim user
Dim monOU
Const ADS_SCOPE_SUBTREE = 2
'déclaration du fichier et du dictionnaire
Set fso = CreateObject("Scripting.FileSystemObject")
Set fichier = fso.OpenTextFile("c:\liste.csv", ForReading)
Set objDictionary = CreateObject("Scripting.Dictionary")
'boucle pour lire le fichier ligne par ligne et stocker dans le dictionnaire
Do While (Not fichier.AtEndOfStream)
Ligne = fichier.Readline
tableau = split(Ligne, ";")
nom = tableau(0)
monOU= tableau(7)
objDictionary.Add nom,nom
objDictionary.ADD monOU,monOU
'If objDictionary.exists(monOU) then wscript.echo "la clé monOU existe et sa valeur est :" & objDictionary.item(monOU)
Loop
'determine le chemin LDAP
Set Root = GetObject("LDAP://RootDSE")
DomainPath = Root.Get("DefaultNamingContext")
'Création objet pour connexion avec active directory
Set objConnection = CreateObject("ADODB.Connection")
'Création d'objet pour faire la requete sur l'active directory
Set objCommand = CreateObject("ADODB.Command")
'paramètre de la connexion
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.Properties("Page Size") = 500
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.CommandText = "SELECT CN FROM 'LDAP://OU=Test,"+DomainPath+"' WHERE objectCategory='user'"
'requete LDAP
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
user = objRecordset.Fields(0).Value
objRecordSet.MoveNext
If Not objDictionary.Exists(user) then Suppr_utilisateur user
Loop
objConnection.close
'fonction pour supprimer l'utilisateur
Function Suppr_utilisateur(nom)
valeur = objDictionary.items
On error Resume Next
For i=0 To ubound(valeur)
Set conteneur = getObject("LDAP://OU=" & (valeur(i)) & "," & "OU=Test" & "," & DomainPath)
conteneur.Delete "user","CN=" & nom
Next
End Function |