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 :

Modifier des comptes utilisateurs Active Directory


Sujet :

VBScript

  1. #1
    Membre actif
    Homme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Décembre 2006
    Messages
    1 080
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : France, Seine Maritime (Haute Normandie)

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

    Informations forums :
    Inscription : Décembre 2006
    Messages : 1 080
    Points : 287
    Points
    287
    Par défaut Modifier des comptes utilisateurs Active Directory
    Bonjour,

    Je souhaite faire un script qui me permet de modifier certains champs de comptes utilisateurs d'Active Directory.

    Pour le moment, j'ai un fichier texte avec les nom des utilisateurs que je parse. J'ai donc le nom de l'utilisateur en variable.

    Comment faire ? Par où commencer ?

    Merci d'avance

  2. #2
    Membre actif
    Homme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Décembre 2006
    Messages
    1 080
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : France, Seine Maritime (Haute Normandie)

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

    Informations forums :
    Inscription : Décembre 2006
    Messages : 1 080
    Points : 287
    Points
    287
    Par défaut
    Salut,

    J'ai avancé sur mon script dont voici un exemple :

    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
     
    Private Function AD_UserDN(sROOT_LDAP,samAccountName)
    	On Error Resume Next
    	Const ADS_SCOPE_SUBTREE = 2
    	Dim oConnection : Set oConnection = CreateObject("ADODB.Connection")
    	Dim oCommand : Set oCommand = CreateObject("ADODB.Command")
    	Dim sDN
    	Dim bGotResult : bGotResult = False
    	oConnection.Provider = ("ADsDSOObject")
    	oConnection.Open "Active Directory Provider"
    	oCommand.ActiveConnection = oConnection
    	oCommand.Properties("Page Size") = 1000
    	oCommand.Properties("Timeout") = 30
    	oCommand.Properties("Cache Results") = False
    	oCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
    	oCommand.CommandText = _
    	"SELECT distinguishedName FROM 'LDAP://" & sROOT_LDAP & "'" _
    	& " WHERE objectClass='user' AND samAccountName = '" & samAccountName & "'"
    	Dim oRecordSet : Set oRecordSet = oCommand.Execute
    	oRecordSet.MoveFirst
    	Do Until oRecordSet.EOF
    		sDN = oRecordSet.Fields("distinguishedName").value
    		bGotResult = True
    		oRecordSet.MoveNext
    	Loop
    	oRecordSet.Close
    	oConnection.Close
    	Set oRecordSet = Nothing
    	Set oCommand = Nothing
    	Set oConnection = Nothing
    	If bGotResult Then
    		AD_UserDN = sDN
    	Else
    		AD_UserDN = "Utilisateur inconnu"
    	End If
    	On Error Goto 0
    End Function
     
    Private Sub PutSomeADParameters(UserDN, UID, GID, Home, Environnement)
    	Dim objUser
    	Set objUser = GetObject _
    		("LDAP://" & UserDN)
    	objUser.Put "uidNumber", UID
    	objUser.Put "gidNumber", GID
    	objUser.Put "unixHomeDirectory", Home
    	objUser.SetInfo
    End Sub
     
     
    Dim fso 
    Dim fCsv 
    Dim tb 
    Const ForReading = 1
    Set fso = CreateObject("scripting.filesystemobject")
    Set fCsv = fso.OpenTextFile("audit_NIS-AD.csv", ForReading)
    If Not fCsv.AtEndOfStream Then fCsv.ReadLine ' lecture ligne d'entête
    While Not fCsv.AtEndOfStream
    	tb = Split(fCsv.ReadLine, ";")
     
    	If UBound(tb) = 12 Then
     
    		If tb(12) = 1 Then
     
    			'Wscript.echo "User : " & tb(0) & "  UID :" & tb(3) & "  GID:" & tb(4) & " Home:" & tb(6) & " Environnement:" & tb(7) & " A migrer:" & tb(12)
     
    			'Option Explicit
    			Dim UserDistinguishedName : UserDistinguishedName = AD_UserDN("DOMAINE.COM",tb(0))
     
    			If UserDistinguishedName = "Utilisateur inconnu" Then
    				Wscript.echo "Utilisateur inconnu"
    			Else
    				PutSomeADParameters UserDistinguishedName,tb(3),tb(4),tb(6),tb(7)
    			End If
    		End If
     
    	End If
    Wend
     
    Wscript.echo "Terminé !"
    Ce script fonctionne correctement. J'arrive bien à mettre à jours certains champs d'objet "utilisateur".

    Cependant, j'aimerai mettre à jour les champs correspondant aux attributs UNIX :



    (Ajouté suite à une extension du schéma d'Active Directory en Win2K3R2)

    J'ai trouvé certains champs comme le UID,GID, Répertoire de base, mais je ne trouve pas les attributs dans le schéma AD pour les champs "Domaine NIS" et "Environnement de démarrage".

    Auriez vous une idée ?

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

    Informations forums :
    Inscription : Avril 2007
    Messages : 340
    Points : 582
    Points
    582
    Par défaut
    Pas d'idée, mais il est fort possible qu'un adsiedit (contenu dans les Windows Support Tools) ou un hyena (très puissant mais payant) te donne ces informations.
    Sinon il faudra attendre le passage d'un connaisseur de cette extension...
    Tiens nous au courant!

  4. #4
    Membre actif
    Homme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Décembre 2006
    Messages
    1 080
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : France, Seine Maritime (Haute Normandie)

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

    Informations forums :
    Inscription : Décembre 2006
    Messages : 1 080
    Points : 287
    Points
    287
    Par défaut
    J'utilise justement ADSI Edit et le MSDN (http://msdn.microsoft.com/en-us/libr...=VS.85%29.aspx) j'ai trouvé pour les attributs correspondant au "UID", à "l'environnement de démarrage", le "Répertoire de base" et le "GID" :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    Private Sub PutSomeADParameters(UserDN, UID, GID, Home, Environnement)
    	Dim objUser
    	Set objUser = GetObject _
    		("LDAP://" & UserDN)
     
    	objUser.Put "uidNumber", UID
    	objUser.Put "loginShell", Environnement
    	objUser.Put "unixHomeDirectory", Home
    	objUser.Put "gidNumber", GID
     
    	objUser.SetInfo
    End Sub
    Mais je ne trouve pas pour l'attribut qui correspond au champ sélectionnable "Domaine NIS". J'ai testé avec "memberNisNetgroup" et "nisMapName", mais ça ne donne rien.

  5. #5
    Membre actif
    Homme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Décembre 2006
    Messages
    1 080
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : France, Seine Maritime (Haute Normandie)

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

    Informations forums :
    Inscription : Décembre 2006
    Messages : 1 080
    Points : 287
    Points
    287
    Par défaut
    finalement j'ai trouvé comme un grand !

    pour le dernier (NIS DOMAIN) l'attribut s'appel : "msSFU30NisDomain"

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    Private Sub PutSomeADParameters(UserDN, UID, GID, Home, Environnement)
    	Dim objUser
    	Set objUser = GetObject _
    		("LDAP://" & UserDN)
     
    	objUser.Put "msSFU30NisDomain", "Domaine_Nis"	
    	objUser.Put "uidNumber", UID
    	objUser.Put "loginShell", Environnement
    	objUser.Put "unixHomeDirectory", Home
    	objUser.Put "gidNumber", GID
     
    	objUser.SetInfo
    End Sub
    et le script 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
     
    Private Function AD_UserDN(sROOT_LDAP,samAccountName)
    	On Error Resume Next
    	Const ADS_SCOPE_SUBTREE = 2
    	Dim oConnection : Set oConnection = CreateObject("ADODB.Connection")
    	Dim oCommand : Set oCommand = CreateObject("ADODB.Command")
    	Dim sDN
    	Dim bGotResult : bGotResult = False
    	oConnection.Provider = ("ADsDSOObject")
    	oConnection.Open "Active Directory Provider"
    	oCommand.ActiveConnection = oConnection
    	oCommand.Properties("Page Size") = 1000
    	oCommand.Properties("Timeout") = 30
    	oCommand.Properties("Cache Results") = False
    	oCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
    	oCommand.CommandText = _
    	"SELECT distinguishedName FROM 'LDAP://" & sROOT_LDAP & "'" _
    	& " WHERE objectClass='user' AND samAccountName = '" & samAccountName & "'"
    	Dim oRecordSet : Set oRecordSet = oCommand.Execute
    	oRecordSet.MoveFirst
    	Do Until oRecordSet.EOF
    		sDN = oRecordSet.Fields("distinguishedName").value
    		bGotResult = True
    		oRecordSet.MoveNext
    	Loop
    	oRecordSet.Close
    	oConnection.Close
    	Set oRecordSet = Nothing
    	Set oCommand = Nothing
    	Set oConnection = Nothing
    	If bGotResult Then
    		AD_UserDN = sDN
    	Else
    		AD_UserDN = "Utilisateur inconnu"
    	End If
    	On Error Goto 0
    End Function
     
    Private function PutSomeADParameters(UserDN, User, UID, GID, Home, Environnement)
    	Dim objUser
    	Set objUser = GetObject _
    		("LDAP://" & UserDN)
     
    	objUser.Put "msSFU30NisDomain", "Ton_Domaine_NIS"	
    	objUser.Put "uidNumber", UID
    	objUser.Put "loginShell", Environnement
    	objUser.Put "unixHomeDirectory", Home
    	objUser.Put "gidNumber", GID
     
    	On Error Resume Next
    		objUser.SetInfo
    	If err.number <> 0 then
    		Wscript.echo User & " : ECHEC > Erreur : Champ incorrecte - " & err.number & " " & err.description
    		PutSomeADParameters = User & ";ECHEC > Erreur : Champ incorrecte - " & err.number & " " & err.description & vbCrLf
    	Else
    		Wscript.echo User & " : Succès"
    		PutSomeADParameters = User & ";SUCCES" & vbCrLf
    	End if			
    End function
     
    Private function writeFile(text,file)
       Dim fso, w 
     
       Set fso = CreateObject("Scripting.FileSystemObject")
       Set w = fso.OpenTextFile(file, 8,true)
       w.write(text)
       w.Close
    end function
     
     
    Dim fso 
    Dim fCsv 
    Dim tb 
    Dim result
    Const ForReading = 1
     
     
    Const DeleteReadOnly = True
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    On Error Resume Next
    	objFSO.DeleteFile("log_Export_NIS_AD.log"), DeleteReadOnly
    On Error Goto 0
     
     
    Set fso = CreateObject("scripting.filesystemobject")
    Set fCsv = fso.OpenTextFile("audit_NIS-AD.csv", ForReading)
    If Not fCsv.AtEndOfStream Then fCsv.ReadLine ' lecture ligne d'entête
    While Not fCsv.AtEndOfStream
    	tb = Split(fCsv.ReadLine, ";")
     
    	If UBound(tb) = 12 Then
     
    		If tb(12) = 1 Then
     
    			'Wscript.echo "User : " & tb(0) & "  UID :" & tb(2) & "  GID:" & tb(3) & " Home:" & tb(5) & " Environnement:" & tb(6) & " A migrer:" & tb(12)
     
    			'Option Explicit
    			Dim UserDistinguishedName : UserDistinguishedName = AD_UserDN("Ton_Domaine_AD",tb(0))
     
    			If UserDistinguishedName = "Utilisateur inconnu" Then
    				Wscript.echo tb(0) & " : Echec > Utilisateur inconnu"
    				writeFile tb(0) & ";Echec > Utilisateur inconnu" & vbCrLf,"log_Export_NIS_AD.log"
    			Else
    				writeFile PutSomeADParameters (UserDistinguishedName,tb(0),tb(2),tb(3),tb(5),tb(6)), "log_Export_NIS_AD.log"
    			End If
    		End If
     
    	End If
    Wend
     
    Wscript.echo
    Wscript.echo "Terminé !"
    et un exemple de fichier CSV :

    Users;Password;SID;GUID;Last name - First name;Home;startupEnvironment;LastConnexionNIS;foundAD;userAccountControlAD;userAccountControlNis;typeAccount;Migrate
    Toto;qeq1xmhS3x9vg;1904;1;Administration Toto;/home/users2/ad-toto;/bin/ksh;12/11/2009;0;0;1;serviceAccount;0
    Voila

  6. #6
    Membre à l'essai
    Inscrit en
    Octobre 2009
    Messages
    21
    Détails du profil
    Informations forums :
    Inscription : Octobre 2009
    Messages : 21
    Points : 14
    Points
    14
    Par défaut
    Bonjour,

    Je suis super interressé par ton script.

    J'ai la meme problematique, je souhaite modifier des utilisateurs existants dans mon AD (comme le prenom le nom etc) a partir d'un ID unique que j'ai generer pour chaque utilisateur dans le champs uid.

    Je voudrai donc faire une recherche via cet id puis editer les informations de l'utilisateur.

    Pourrais tu m'indiquer comment ton script fonctionne ? car je pense qu'il s'agit de la meilleure methode par rapport a ce que je souhaite faire.

    PS : je suis pas tres bon en vbs c'est pourquoi je ne sais pas comment utiliser tes fonction avec ton csv, si tu pouvais m'expliquer ta manip ca serai top

    Merci

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

Discussions similaires

  1. Gestion des utilisateurs : active directory & base de données
    Par Ragnarock dans le forum ASP.NET MVC
    Réponses: 2
    Dernier message: 26/08/2011, 09h38
  2. Extraction des utilisateur Active directory
    Par charouel dans le forum Windows Forms
    Réponses: 0
    Dernier message: 14/01/2010, 09h45
  3. [Windows 2003] Compte et Droits utilisateurs - Active Directory
    Par Stef.web dans le forum Windows Serveur
    Réponses: 3
    Dernier message: 26/08/2008, 16h17
  4. Réponses: 15
    Dernier message: 22/03/2007, 16h48
  5. Compte utilisateur Active Directory
    Par dim971 dans le forum C#
    Réponses: 9
    Dernier message: 11/02/2007, 19h37

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