Bonjour,

Je débute dans le vbscript, mais pour une première je dois faire fort

Voici le script de création d'utilisateurs Active Directory que j'ai créé.


creation_users.vbs
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
 
' Script de gestion d'utilisateurs
' Créé le 18/07/2007 par Jérôme Seyler
 
' Variables globales'
 
'Fichier contenant les comptes utilisateurs'
const fichier = "c:\utilisateurs.csv"
'Repertoires de données'
'a modifier par e:\ sur le serveur définitif car baie de stockage connectée en E:'
const datadrive = "c:\"
'Répertoire contenant les données sur le lecteur spécifié'
const datafolder = "donnees"
'Construction du répertoire de base'
basePath = datadrive & datafolder
 
'Serveurs'
const serveur1="\\srvstockage"
 
'Déclaration des différentes variables'
Dim FSO
Dim nom
Dim prenom
Dim etablissement
Dim niveau
Dim classe
Dim login
Dim mdp
Dim groupe
Dim sDN
Dim laOU
Dim userPath
Dim groupeprofs
 
Set FSO = CreateObject("Scripting.FileSystemObject")
Set Ftxt = FSO.OpenTextFile(fichier)
 
Do While (Not Ftxt.AtEndOfStream)
  Ligne = Ftxt.Readline
  LigneDecoupee = split(Ligne,chr(59))
  nom = LigneDecoupee(0)
  prenom = LigneDecoupee(1)
  etablissement = LigneDecoupee(2)
  niveau = LigneDecoupee(3)
  classe = LigneDecoupee(4)
  login = LigneDecoupee(5)
  'login = ucase(prenom) + "." + ucase(nom)
  mdp = LigneDecoupee(6)
 
  if NOT ((lcase(nom) = "nom") OR (lcase(prenom) = "prenom")) then
    'Appel des différentes fonctions'
    Call CreerCompte()
    Call CreerArboUser()
    Call CreerArboGenerale()
    Call PermissionsPerso()  
    Call PermissionsPublic()
  else
    'msgbox (nom & "pas d'appel aux fonctions")
  end if
 
Loop
 
    Call CreationPartage()
    Call CreationPartageAdmins()
    Call CreationPartageProfs()
    Call CreationPartageLycee()
    Call CreationPartageCollege()
 
'Fermeture du fichier'
Ftxt.Close
 
 
' Debut fonction CreerCompte
function CreerCompte()
  'msgbox (nom + Prenom + Etablissement + Niveau + Classe + login + mdp)
  '// Récupération du root 
  Set oRoot = GetObject("LDAP://rootDSE") 
  sRoot = oRoot.Get("defaultNamingContext")
  On Error Resume Next 
 
  Set oDomain = GetObject("LDAP://" & sRoot)
 
  'la OU institut doit exister'
  if ((etablissement="Professeurs") or (etablissement="Administratifs")) then
    laOU = ("ou=" & etablissement & ",ou=Institut")
    Set oOU = oDomain.Create("organizationalUnit", laOU)
    oOU.SetInfo
  else
    laOU = ("ou=" & classe & ",ou=" & niveau & ",ou=" & etablissement & ",ou=Institut")
    Set oOU = oDomain.Create("organizationalUnit", "ou=" & etablissement & ",ou=Institut")
    oOU.SetInfo
    Set oOU = oDomain.Create("organizationalUnit", "ou=" & niveau & ",ou=" & etablissement & ",ou=Institut")
    oOU.SetInfo
    Set oOU = oDomain.Create("organizationalUnit", "ou=" & classe & ",ou=" & niveau & ",ou=" & etablissement & ",ou=Institut")
    oOU.SetInfo
    Set oOU = oDomain.Create("organizationalUnit", laOU)
    oOU.SetInfo
  end if
 
  'msgbox("LDAP://" & laOU & "," & sRoot)
  Set oOU = GetObject("LDAP://" & laOU & "," & sRoot)
 
  Set oUser = oOU.Create("user", "cn=" & login)
 
  '// Load fields in AD record 
  oUser.put "sAMAccountName", lcase(login) 
  oUser.put "givenName", prenom
  oUser.put "sn", nom
  oUser.put "UserPrincipalName", lcase(login) 
  oUser.put "DisplayName", prenom & " " & nom
  oUser.put "Description", "Utilisateur " & prenom & " " & nom
 
  'Gestion des groupes'
  if ((etablissement="Lycée")Or(etablissement="Collège")) then
      groupeeleves = "Elèves" & niveau & classe
      groupeprofs = "Professeurs" & niveau & classe
     'Création du groupe Eleves'
      Set oGroup = oOU.Create("Group", "cn=" & groupeeleves)
      oGroup.Put "sAMAccountName", groupeeleves
      oGroup.SetInfo
      oGroup.Add groupeeleves
      oGroup.SetInfo
      oGroup.Add oUser.ADSPath
      oGroup.SetInfo
 
      'Création du groupe Profs de la classe'
      Set oGroup = oOU.Create("Group", "cn=" & groupeprofs)
      oGroup.Put "sAMAccountName", groupeprofs
      oGroup.SetInfo
      oGroup.Add groupeprofs
      oGroup.SetInfo
 
      'Assignation du lecteur P: au répertoire public pour client lourd'
      oUser.put "ProfilePath", serveur1 & "\" & datafolder & "\" & etablissement & "\" & niveau & "\" & classe & "\" '& username
      oUser.put "HomeDirectory", serveur1 & "\" & datafolder & "\" & etablissement & "\" & niveau & "\" & classe & "\public"
      oUser.Put "HomeDrive", "P:"
      oUser.SetInfo
 
      'Assignation du lecteur P: au répertoire public pour client leger'
      oUser.TerminalServicesProfilePath = serveur1 & "\" & datafolder & "\" & etablissement & "\" & niveau & "\" & classe & "\" '& username
      oUser.TerminalServicesHomeDirectory = serveur1 & "\" & datafolder & "\" & etablissement & "\" & niveau & "\" & classe & "\public"
      oUser.TerminalServicesHomeDrive = "P:"
      oUser.AllowLogon = 1
      oUser.SetInfo
 
  elseif ((etablissement="Professeurs")Or(etablissement="Administratifs")) then
      groupepersonnel = etablissement
      'msgbox groupe
      'Création du groupe Profs'
      Set oGroup = oOU.Create("Group", "cn=" & groupepersonnel)
      oGroup.Put "sAMAccountName", groupepersonnel
      oGroup.SetInfo
      oGroup.Add groupepersonnel
      oGroup.SetInfo
      oGroup.Add oUser.ADSPath
      oGroup.SetInfo
 
      'Assignation du lecteur P: au répertoire public pour un client lourd'
      oUser.put "ProfilePath", serveur1 & "\" & datafolder & "\" & etablissement & "\" '& username
      oUser.put "HomeDirectory", serveur1 & "\" & datafolder & "\" & etablissement & "\public"
      oUser.Put "HomeDrive", "P:"
      oUser.SetInfo
 
      'Assignation du lecteur P: au répertoire public pour un client leger'
      oUser.TerminalServicesProfilePath = serveur1 & "\" & datafolder & "\" & etablissement & "\" '& username      
      oUser.TerminalServicesHomeDirectory = serveur1 & "\" & datafolder & "\" & etablissement & "\public"
      oUser.TerminalServicesHomeDrive = "P:"
      oUser.AllowLogon = 1
      oUser.SetInfo
 
  else
      'msgBox ("Groupe inconnu") 
  end if
 
  oUser.setpassword mdp
  oUser.SetInfo 
 
  oUser.put "userAccountControl", 512
  oUser.SetInfo
 
 
  '// Set the User's DN for adding to group 
  sDN = oUser.get("DistinguishedName") 
 
end function
' Fin fonction CreerCompte
 
' Debut fonction CreerArboUser
function CreerArboUser()
 
  'Récupération du dossier selon si l'utilisateur est un élève ou un professeur'
  if ((etablissement="Professeurs") OR (etablissement="Administratifs")) then
    userPath = basePath & "\" & etablissement
  else
    userPath = basePath & "\" & etablissement & "\" & niveau & "\" & classe 
  end if
 
  'test de l'existance du dossier'
  Set objbasePath = CreateObject("Scripting.FileSystemObject")
    If objbasePath.FolderExists(basePath) Then
        'msgbox ("Le dossier existe deja.")
      Else
        'msgbox ("Création du dossier.")
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set objbasePath = objFSO.CreateFolder(basePath)
    End If
 
  'msgbox (userPath)
  Set objPath = CreateObject("Scripting.FileSystemObject")
    If objPath.FolderExists(userPath) Then
        'msgbox ("Le dossier existe deja.")
      Elseif ((etablissement="Professeurs") OR (etablissement="Administratifs")) then 
        'msgbox ("Création du dossier.")
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set objPath = objFSO.CreateFolder(basePath & "\" & etablissement)
        Set objPath = objFSO.CreateFolder(basePath & "\" & etablissement & "\" & login)
      else
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set objPath = objFSO.CreateFolder(basePath & "\" & etablissement)
        Set objPath = objFSO.CreateFolder(basePath & "\" & etablissement & "\" & niveau)
        Set objPath = objFSO.CreateFolder(basePath & "\" & etablissement & "\" & niveau & "\" & classe)
        Set objPath = objFSO.CreateFolder(basePath & "\" & etablissement & "\" & niveau & "\" & classe & "\" & login)
    End If
end function
' Fin fonction CreerArboUser
 
' Debut fonction CreerArboGenerale 
function CreerArboGenerale()
  On Error Resume Next 
 
  'test de l'existance du dossier'
  Set objbasePath = CreateObject("Scripting.FileSystemObject")
    If objbasePath.FolderExists(basePath) Then
        'msgbox ("Le dossier existe deja.")
      Else
        'msgbox ("Création du dossier.")
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set objbasePath = objFSO.CreateFolder(basePath)
    End If
 
  'Récupération du dossier selon si l'utilisateur est un élève ou un professeur'
  if ((etablissement="Professeurs") OR (etablissement="Administratifs")) then
    userpubPath = basePath & "\" & etablissement & "\public"
  else
    userpubPath = basePath & "\" & etablissement & "\" & niveau & "\" & classe & "\public"
  end if
 
  'msgbox (userPath)
  Set objPath = CreateObject("Scripting.FileSystemObject")
    If objPath.FolderExists(userpubPath) Then
        'msgbox ("Le dossier existe deja.")
      Elseif ((etablissement="Professeurs") OR (etablissement="Administratifs")) then 
        'msgbox ("Création du dossier.")
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set objPath = objFSO.CreateFolder(basePath & "\" & etablissement)
        Set objPath = objFSO.CreateFolder(basePath & "\" & etablissement & "\public")
      else
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set objPath = objFSO.CreateFolder(basePath & "\" & etablissement)
        Set objPath = objFSO.CreateFolder(basePath & "\" & etablissement & "\" & niveau)
        Set objPath = objFSO.CreateFolder(basePath & "\" & etablissement & "\" & niveau & "\" & classe)
        Set objPath = objFSO.CreateFolder(basePath & "\" & etablissement & "\" & niveau & "\" & classe & "\public")
    End If
 
end function
' Fin fonction CreerArboGenerale
 
'Début fonction PermissionsPerso'
'Autorise un acces complet a chaque dossier personnel des utilisateurs'
function PermissionsPerso()
    Dim objShell, intRunError
    'Msgbox etablissement
    'Recupération de l'arbo du répertoire perso'
    strHomeFolder = userPath & "\" & login
    strSamAccountName = login
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objShell = CreateObject("Wscript.Shell")
    ' On assigne la permission RWX d'un utilisateur à son répertoire perso et l'acces complet aux administrateurs
    If objFSO.FolderExists(strHomeFolder) Then
      'ATTENTION IL NE FAUT PAS D'ESPACE ENTRE LE O ET LE | !!!!! (MS KB 135268)'
        if  (etablissement="Administratifs" OR etablissement="Professeurs") then
          'msgbox "admin ou prof"
          'intRunError = objShell.Run("%COMSPEC% /c Echo O| cacls " & strHomeFolder & " /t /c /g Administrateurs:F " & strSamAccountName & ":C")
          intRunError = objShell.Run("%COMSPEC% /c Echo O| cacls " & strHomeFolder & " /t /c /g Administrateurs:F " & strSamAccountName & ":C",2,True)
        else  
          'msgbox "eleve"
          'intRunError = objShell.Run("%COMSPEC% /c Echo O| cacls " & strHomeFolder & " /t /c /g Administrateurs:F " & groupeprofs & ":C " & strSamAccountName & ":C")
          intRunError = objShell.Run("%COMSPEC% /c Echo O| cacls " & strHomeFolder & " /t /c /g Administrateurs:F " & groupeprofs & ":C " & strSamAccountName & ":C",2,True)
        end if
      'msgbox ("cacls " & strHomeFolder & " /t /c /g Administrateurs:F /p " & groupeprofs & ":C " & strSamAccountName & ":C")
      If intRunError < 0 Then
        Wscript.Echo "Erreur a la creation des permissions pour l'utilisateur " & strSamAccountName & " sur le répertoire perso " & strHomeFolder
      Else
        'Wscript.Echo "Permissions successfully assigned for user " & strSamAccountName & " to home folder " & strHomeFolder
      End If
    Else
      Wscript.Echo "Folder " & strHomeFolder & " does not exist."
    End If
    Set objFSO = Nothing
    Set objShell = Nothing 
end function
'Fin fonction PermissionsPerso'
 
'Début fonction PermissionsPublic'
function PermissionsPublic()
    Dim objShell, intRunError
    'Msgbox etablissement
    'Recupération de l'arbo du répertoire perso'
    strPublicFolder = userPath & "\public"
    'msgbox groupeprofs
    strSamAccountName = login
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objShell = CreateObject("Wscript.Shell")
    ' On assigne la permission RWX au répertoire public selon les groupes et l'arbo et l'acces complet aux administrateurs
    If objFSO.FolderExists(strPublicFolder) Then
      'ATTENTION IL NE FAUT PAS D'ESPACE ENTRE LE O ET LE | !!!!! (MS KB 135268)'
        if  (etablissement="Administratifs" OR etablissement="Professeurs") then
          'msgbox strPublicFolder
          intRunError = objShell.Run("%COMSPEC% /c Echo O| cacls " & strPublicFolder & " /t /c /g Administrateurs:F Administratifs:C Professeurs:C",2,True)
        else
          'msgbox strPublicFolder
          intRunError = objShell.Run("%COMSPEC% /c Echo O| cacls " & strPublicFolder & " /t /c /g Administrateurs:F " & groupeprofs & ":C " & groupe & ":C",2,True)
        end if
      If intRunError < 0 Then
        Wscript.Echo "Erreur a la creation des permissions pour l'utilisateur " & strSamAccountName & " sur le répertoire perso " & strPublicFolder
      Else
        'Wscript.Echo "Permissions successfully assigned for user " & strSamAccountName & " to Public folder " & strPublicFolder
      End If
    Else
      Wscript.Echo "Folder " & strPublicFolder & " does not exist."
    End If
    Set objFSO = Nothing
    Set objShell = Nothing 
end function
'Fin fonction PermissionsPublic'
 
'Ajouter fonction restriction acces répertoires administratifs et profs (en ajoutant tous les groupes eleves dans un groupes eleves generique ?'
 
'Fonction de creation du partage général'
function CreationPartage()
  Const FILE_SHARE = 0
  Const MAXIMUM_CONNECTIONS = 25
  strComputer = "."
  Set objWMIService = GetObject("winmgmts:" _
      & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
  Set objNewShare = objWMIService.Get("Win32_Share")
  errReturn = objNewShare.Create _
      ("C:\donnees", "donnees", FILE_SHARE, _
          MAXIMUM_CONNECTIONS, "Partage racine")
  'Wscript.Echo errReturn
 
end function
'Fonction de creation du partage général'
 
'Fonction de creation du partage administratifs'
function CreationPartageAdmins()
  Const FILE_SHARE = 0
  Const MAXIMUM_CONNECTIONS = 25
  strComputer = "."
  Set objWMIService = GetObject("winmgmts:" _
      & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
  Set objNewShare = objWMIService.Get("Win32_Share")
  errReturn = objNewShare.Create _
     (basepath & "\Administratifs", "Administratifs", FILE_SHARE, _
          MAXIMUM_CONNECTIONS, "Partage administratifs")
  'Wscript.Echo errReturn
end function
'Fonction de creation du partage administratif'
 
'Fonction de creation du partage Professeurs'
function CreationPartageProfs()
  Const FILE_SHARE = 0
  Const MAXIMUM_CONNECTIONS = 25
  strComputer = "."
  Set objWMIService = GetObject("winmgmts:" _
      & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
  Set objNewShare = objWMIService.Get("Win32_Share")
  errReturn = objNewShare.Create _
      (basepath & "\Professeurs", "Professeurs", FILE_SHARE, _
          MAXIMUM_CONNECTIONS, "Partage professeurs")
  'Wscript.Echo errReturn
end function
'Fonction de creation du partage professeurs'
 
'Fonction de creation du partage Collège'
function CreationPartageCollege()
  Const FILE_SHARE = 0
  Const MAXIMUM_CONNECTIONS = 25
  strComputer = "."
  Set objWMIService = GetObject("winmgmts:" _
      & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
  Set objNewShare = objWMIService.Get("Win32_Share")
  errReturn = objNewShare.Create _
      (basepath & "\Collège", "Collège", FILE_SHARE, _
          MAXIMUM_CONNECTIONS, "Partage collègiens")
  'Wscript.Echo errReturn
end function
'Fonction de creation du partage college'
 
'Fonction de creation du partage lycee'
function CreationPartageLycee()
  Const FILE_SHARE = 0
  Const MAXIMUM_CONNECTIONS = 25
  strComputer = "."
  Set objWMIService = GetObject("winmgmts:" _
      & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
  Set objNewShare = objWMIService.Get("Win32_Share")
  errReturn = objNewShare.Create _
      (basepath & "\Lycée", "Lycée", FILE_SHARE, _
          MAXIMUM_CONNECTIONS, "Partage lycéens")
  'Wscript.Echo errReturn
end function
'Fonction de creation du partage administratif'
utilisateurs.csv
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
 
DURAND;Paul;Collège;Seconde;Rouge;p.durand;Z12345a
DUPONT;Céline;Lycée;Quatrième;Bleu;c.dupont;123
DAVID;Franck;Administratifs;Troisième;Vert;f.david;123
SEYLER;Jérôme;Professeurs;Seconde;Vert;j.seyler;123
Mon problème se situe à l'affectation d'un utilisateur à un groupe. En fait, mes utilisateurs et mes groupes sont correctement créés, mais il faut un 2eme passage (relancer le script une 2eme fois) pour affecter le compte au groupe qui vient d'être créé...

Je suis également preneur d'éventuelles optimisations mais ce n'est pas le but premier.

Avez vous une idée ? C'est relativement urgent, je dois terminer cela pour la fin de la semaine.

Merci d'avance,

Jérôme