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
| Dim user, computer
Dim u,v,w
Dim oRootDSE,strDomainNC
Dim oConnection,oRecordset
Sub recuperation_donnees()
' Recherche du domaine Active Directory courant
Set oRootDSE = GetObject("LDAP://RootDSE")
strDomainNC = oRootDSE.Get("defaultNamingContext")
' Connexion de type base de donnée à Active Directory
Set oConnection = CreateObject("ADODB.Connection")
oConnection.Provider = "ADsDSOObject"
oConnection.Open "ADs Provider"
Dim nom_complet, prenom
'On recherche tous les utilisateurs dans tous les niveaux de larborescence.
Set oRecordset = oConnection.Execute("<GC://"+StrDomainNC+">;(&(objectCategory=user));distinguishedName,sn,givenName,name,displayname,samaccountname;subtree")
' Boucle sur les enregistrements.
If Not oRecordset.EOF Then
While Not oRecordset.EOF
' Les champs vides ne peuvent pas être concaténés
nom_LDAP=VerificationNul(oRecordSet.Fields("distinguishedName"))
nom_complet_actuel=VerificationNul(oRecordSet.Fields("name"))
nom_famille=VerificationNul(oRecordset.Fields("sn"))
prenom=VerificationNul(oRecordset.Fields("givenName"))
If (nom_famille="" or prenom="") Then
nom_LDAP = "erreur"
Else
nom_complet = normalisation_nom_prenom (nom_famille,prenom)
if NOT (nom_complet = nom_complet_actuel) then
res=MiseAJourCompte(nom_LDAP, nom_complet_actuel, nom_complet)
End If
End If
oRecordset.movenext
Wend
End If
End Sub
'fonction permettant de faire le changement
Function MiseAJourCompte(n_LDAP, n_c_actuel, n_complet)
'modification du nom_complet pour obtenir la LDAP de l'OU
pour_OU="CN=" & n_c_actuel & "," 'remise dans le contexte pour connaitre la longueur de la chaine
nb=len(n_LDAP)-len(pour_OU)
'on retire la partie CN=Nom_Utilisateur,
pour_OU=Right(n_LDAP, nb)
'wscript.echo "nombre " & nb & " n_LDAP " & n_LDAP & " LDAP_OU " & pour_OU
fin = "CN=" & n_complet
Set objNewUser = GetObject("LDAP://"& pour_OU)
Set objMoveUser = objNewUser.MoveHere ("LDAP://" & n_LDAP, fin)
UpdateAccount=1
End Function
'fonction permettant de vérifier si le champ est nul ou pas
Function VerificationNul(champ)
If not (isnull(champ)) Then
VerificationNul=champ
Else
VerificationNul=""
End If
End Function
'fonction permettant de normaliser le nom complet suivant la politique d'annuaire
Function normalisation_nom_prenom(nom, prenom)
nb = len(prenom)
prenom = UCASE(LEFT(prenom,1)) & LCASE(RIGHT(prenom,nb-1)) 'prends la première lettre
nom = UCASE(nom)
nom_complet = nom & " " & prenom
'wscript.echo "Nom complet : " & nom & " " & prenom
normalisation_nom_prenom=nom_complet
End Function
'fonction replace ne fonctionne pas en VBS !
Function Remplacer(ancienne_chaine, a_replacer, remplacement)
Dim pos
s = s & ""
pos = InStr(s, Str1)
While pos > 0
s = Mid(s, 1, pos - 1) & Str2 & Mid(s, pos + Len(Str1))
pos = InStr(pos, s, Str1)
Wend
Remplacer = s
End Function
Function Supprimer(ancienne_chaine, a_supprimer)
Dim pos
s = s & ""
pos = InStr(s, Str1)
While pos > 0
s = Mid(s, 1, pos - 1) & Mid(s, pos + Len(Str1))
pos = InStr(pos, s, Str1)
Wend
Supprimer = s
End Function
recuperation_donnees() |
Partager