Script création utulisateur AD Win 2003

90frag Messages postés 4 Date d'inscription lundi 9 mai 2005 Statut Membre Dernière intervention 8 avril 2006 - 16 mars 2006 à 14:33
90frag Messages postés 4 Date d'inscription lundi 9 mai 2005 Statut Membre Dernière intervention 8 avril 2006 - 20 mars 2006 à 08:13
Bonjour,

J'esseye de créer des utilsateurs dans mon AD grace au script suivant:
Je ne suis pas la source de ce script, mais il m'a inspirer pour le construire.
----------------------------------------------------------------------------------
Option Explicit

Dim objExcel, strExcelPath, objSheet
Dim strLast, strFirst, strDescription, strPW, intRow, intCol
Dim objUser, objGroup, objContainer
Dim strCN, strNTName, strContainerDN, strDisplayName
Dim strHomeFolder, strHomeDrive, strProfilePath, objFSO, objShell
Dim intRunError, strNetBIOSDomain, strDNSDomain
Dim objRootDSE, objTrans, strLogonScript, strUPN
Dim strDescription2

Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1

strExcelPath = "c:\createusers.xls"

strContainerDN = "cn=Users,dc=zuider,dc=com"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Wscript.Shell")

Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("DefaultNamingContext")

Set objTrans = CreateObject("NameTranslate")
objTrans.Init ADS_NAME_INITTYPE_GC, ""
objTrans.Set ADS_NAME_TYPE_1779, strDNSDomain
strNetBIOSDomain = objTrans.Get(ADS_NAME_TYPE_NT4)

strNetBIOSDomain = Left(strNetBIOSDomain, Len(strNetBIOSDomain) - 1)

Set objExcel = CreateObject("Excel.Application")

On Error Resume Next
objExcel.Workbooks.Open strExcelPath
If Err.Number <> 0 Then
On Error GoTo 0
Wscript.Echo "Unable to open spreadsheet " & strExcelPath
Wscript.Quit
End If
On Error GoTo 0
Set objSheet = objExcel.ActiveWorkbook.Worksheets(1)

On Error Resume Next
Set objContainer = GetObject("LDAP://" & strContainerDN)
If Err.Number <> 0 Then
On Error GoTo 0
Wscript.Echo "Unable to bind to container: " & strContainerDN
Wscript.Quit
End If
On Error GoTo 0

intRow = 2

Do While objSheet.Cells(intRow, 5).Value <> ""

strFirst = Trim(objSheet.Cells(intRow, 1).Value)
strDescription = Trim(objSheet.Cells(intRow, 2).Value)
strLast = Trim(objSheet.Cells(intRow, 3).Value)
strPW = Trim(objSheet.Cells(intRow, 4).Value)
strCN = Trim(objSheet.Cells(intRow, 5).Value)
strNTName = Trim(objSheet.Cells(intRow, 6).Value)
strUPN = Trim(objSheet.Cells(intRow, 7).Value)
strHomeFolder = Trim(objSheet.Cells(intRow, 8).Value)
strHomeDrive = Trim(objSheet.Cells(intRow, 9).Value)
strProfilePath = Trim(objSheet.Cells(intRow, 10).Value)
strDisplayName = Trim(objSheet.Cells(intRow, 11).Value)
strDescription2 = Trim(objSheet.Cells(intRow, 12).Value)

On Error Resume Next
Set objUser = objContainer.Create("user", "cn=" & strCN)
If Err.Number <> 0 Then
On Error GoTo 0
Wscript.Echo "Unable to create user with cn: " & strCN
Else
On Error GoTo 0

If strNTName = "" Then
strNTName = strCN
End If
objUser.sAMAccountName = strNTName
On Error Resume Next
objUser.SetInfo
If Err.Number <> 0 Then
On Error GoTo 0
Wscript.Echo "Unable to create user with NT name: " & strNTName
Else

objUser.SetPassword strPW
If Err.Number <> 0 Then
On Error GoTo 0
Wscript.Echo "Unable to set password for user " & strNTName
End If
On Error GoTo 0

objUser.AccountDisabled = False
If strFirst <> "" Then
objUser.givenName = strFirst
End If

If strDescription <> "" Then
objUser.initials = strDescription
End If
If strLast <> "" Then
objUser.sn = strLast
End If
If strUPN <> "" Then
objUser.userPrincipalName = strUPN
End If
If strHomeDrive <> "" Then
objUser.homeDrive = strHomeDrive
End If
If strHomeFolder <> "" Then
objUser.homeDirectory = strHomeFolder
End If
If strLogonScript <> "" Then
objUser.scriptPath = strLogonScript
End If
If strProfilePath <> "" Then
objUser.profilePath = strProfilePath
End If
If strDisplayName <> "" Then
objUser.DisplayName = strDisplayName
End If
If strDescription2 <> "" Then
objUser.description = strDescription2
End If
objUser.pwdLastSet = 0

objuser.userAccountControl = 512

On Error Resume Next
objUser.SetInfo
If Err.Number <> 0 Then
On Error GoTo 0
Wscript.Echo "Unable to set attributes for user with NT name: " _
& strNTName
End If
On Error GoTo 0

If strHomeFolder <> "" Then
If Not objFSO.FolderExists(strHomeFolder) Then
On Error Resume Next
objFSO.CreateFolder strHomeFolder
If Err.Number <> 0 Then
On Error GoTo 0
Wscript.Echo "Unable to create home folder: " & strHomeFolder
End If
On Error GoTo 0
End If
If objFSO.FolderExists(strHomeFolder) Then

intRunError = objShell.Run("%COMSPEC% /c Echo Y| cacls " _
& strHomeFolder & " /T /E /C /G " & strNetBIOSDomain _
& "" & strNTName & ":F", 2, True)
If intRunError <> 0 Then
Wscript.Echo "Error assigning permissions for user " _
& strNTName & " to home folder " & strHomeFolder
End If
End If
End If

' intCol = 12
' Do While objSheet.Cells(intRow, intCol).Value <> ""
' strGroupDN = Trim(objSheet.Cells(intRow, intCol).Value)
' On Error Resume Next
' Set objGroup = GetObject("LDAP://" & strGroupDN)
' If Err.Number <> 0 Then
' On Error GoTo 0
' Wscript.Echo "Unable to bind to group " & strGroupDN
' Else
' objGroup.Add objUser.AdsPath
' If Err.Number <> 0 Then
' On Error GoTo 0
' Wscript.Echo "Unable to add user " & strNTName _
'& " to group " & strGroupDN
'End If
'End If
'On Error GoTo 0

'intCol = intCol + 1
'Loop
End If
End If

intRow = intRow + 1
Loop

Wscript.Echo "Done"

objExcel.ActiveWorkbook.Close
objExcel.Application.Quit
Set objUser = Nothing
Set objGroup = Nothing
Set objContainer = Nothing
Set objSheet = Nothing
Set objExcel = Nothing
Set objFSO = Nothing
Set objShell = Nothing
Set objTrans = Nothing
Set objRootDSE = Nothing
-------------------------------------------------------------------------------------------------

Pour créer des utilisateurs, il s'appuis sur le fichier excel c:\createusers.xls
Il marche correctement pour le premier user, il lit les infomrations de la seconde ligne du tableau et créer l'utilisateur.
Il cré ensuite les autres utilisateur mais indique un echec "Unable to create user with NT name: " et laisse les champs des utilisateur vide, les rendant inutilisable.
Si des erreur vous saute au yeux, merci de bien vouloir m'aiguiller.

Cordialement;

1 réponse

90frag Messages postés 4 Date d'inscription lundi 9 mai 2005 Statut Membre Dernière intervention 8 avril 2006
20 mars 2006 à 08:13
Bonjour;

Avancement dans le projet....
Le probleme de non avancement dans la fichier excel venait d'une bete limitation Windows de 8 carateres.

Maintenant, je bloque sur la création et la modification des droits du home folder.
Sa création bloque au second utilisateur.
Pourquoi la commande cacls bloque au second utilisateur de mon fichier?

Cordialement,;
0
Rejoignez-nous