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
| Option Explicit
'On error Resume Next
'déclaration des variables
Dim fso
Dim fichier
Dim templog
Dim fichierlog
Const ForReading = 1
Const ForWriting = 8
Const TristateUseDefault = -2
Dim tableau
Dim Ligne
Dim Root
Dim DomainPath
Dim objConnection
Dim objCommand
Dim objRecordSet
Dim organisation
Dim test_description
Dim nom_complet, account, login, nom, prenom, display, password, unite_org, description
Const ADS_SCOPE_SUBTREE = 2
'déclaration du fichier csv
Set fso = CreateObject("Scripting.FileSystemObject")
Set fichier = fso.OpenTextFile("c:\liste.csv", ForReading)
'détermine le fichier de log
set fichierlog = fso.Getfile("c:\log.txt")
set templog = fichierlog.OpenAsTextStream(ForWriting,TristateUseDefault)
'boucle pour lire le fichier ligne par ligne
Do While (Not fichier.AtEndOfStream)
Ligne = fichier.Readline
tableau = split(Ligne, ";")
test_user tableau(0)
Loop
function test_user (test_description)
'determine le chemin LDAP
organisation = "Test"
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
'requete LDAP pour déterminer si l'utilisateur existe ou pas
objCommand.CommandText = "<LDAP://OU=" & organisation & "," & DomainPath & ">;(&(objectCategory=User)" & _
"(description=" & test_description & "));description;subtree"
Set objRecordSet = objCommand.Execute
'boucle qui créer l'utilisateur ou qui modifie ses attributs
If objRecordset.RecordCount = 0 then
'Cree_Utilisateur test_description, tableau(1), tableau(2), tableau(3), tableau(4), tableau(5), tableau(6), tableau(7), tableau(8)
msgbox "test ok "
Else
'Modif_Utilisateur test_description, tableau(1), tableau(2), tableau(3), tableau(4), tableau(5), tableau(6), tableau(7), tableau(8)
msgbox "test nul "
End If
'ferme la connexion avec l'active directory
objConnection.close
End function
Function Cree_Utilisateur(description, nom_complet, account, login, nom, prenom, display, password, unite_org)
'Indique l'OU de l'utilisateur
Dim chemin
organisation = "Test"
Set chemin = GetObject("LDAP://OU=" & unite_org & "," & "OU=" & organisation & "," & DomainPath)
'Crée l'utilisateur dans l'OU
Dim User
Set User = chemin.Create("user", "CN=" & nom_complet)
'Définit le nom d'ouverture de session
User.Put "sAMAccountName", account
'Définit le nom d'ouverture de session (antérieur à windows 2000)
User.Put "userPrincipalName", login
'Nom de l'utilisateur
User.Put "sn", nom
'Prénom de l'utilisateur
User.Put "givenName", prenom
'Nom affiché
User.Put "displayName", display
'Description de l'utilisateur
User.Put "description", description
User.SetInfo
'mot de passe n'expire jamais
const ADS_UF_DONT_EXPIRE_PASSWD = &H10000
User.Put "userAccountControl",ADS_UF_DONT_EXPIRE_PASSWD
'définit le mot de passe
User.SetPassword password
'Active le compte
User.AccountDisabled = False
User.SetInfo
'écrire dans le fichier log la réussite de la création de l'utilisateur
Templog.Writeline "l'utilisateur " & nom & " " & prenom & " a été créé avec succés dans l'OU " & unite_org
End Function
Function Modif_Utilisateur(description, nom_complet, account, login, nom, prenom, display, password, unite_org)
Const ADS_PROPERTY_UPDATE = 2
'Indique l'OU de l'utilisateur et l'utilisateur
Dim User
organisation = "Test"
Set User = GetObject("LDAP://description=" & description & "," & "OU=" & unite_org & "," & "OU=" & organisation & "," & DomainPath)
'Redéfinit le CN de l'utilisateur
User.PutEx ADS_PROPERTY_UPDATE, "CN", Array("" & nom_complet)
'Redéfinit le nom d'ouverture de session
User.PutEx ADS_PROPERTY_UPDATE, "sAMAccountName", Array("" & account)
'Redéfinit le nom d'ouverture de session (antérieur à windows 2000)
User.PutEx ADS_PROPERTY_UPDATE, "userPrincipalName", Array("" & login)
'Redéfinit le Nom de l'utilisateur
User.PutEx ADS_PROPERTY_UPDATE, "sn", Array("" & nom)
'Redéfinit le Nom affiché sur windows
User.PutEx ADS_PROPERTY_UPDATE, "displayName", Array("" & display)
User.SetInfo
'définit le mot de passe
User.SetPassword password
User.SetInfo
End Function |