‘ Part 1: Use ADO to open the Excel spreadsheet. Dim oCN Set oCN = CreateObject("ADODB.Connection") oCN.Open "Excel" Dim oRS Set oRS = oCN.Execute("SELECT * FROM [Sheet1$]") ‘ Part 2: Use ADSI to obtain a reference to the NT domain. Dim oDomain Dim sPDC sPDC = "NT4PDC" Set oDomain =
LISTING : Bulk-users.vbs
![LISTING : Bulk-users.vbs LISTING : Bulk-users.vbs](https://www.itpro.fr/wp-content/uploads/2015/03/36e1ba240a3989c03573f9b5794f293d.jpg)
GetObject(« WinNT:// » & sPDC)
‘ Part 3: Open an output text file to store users initial passwords.
Dim oFSO, oTS
Set oFSO = CreateObject(« Scripting.FileSystemObject »)
Set oTS = oFSO.CreateTextFile(« C:\passwords.txt »,True)
‘ Part 4: For each record in the record set, add the user, set the correct user
‘ properties, and add the user to the appropriate groups.
‘ Create the necessary variables.
Dim sUserID, sFullName, sDescription
Dim sHomeDir, sGroups, sDialIn
Dim sPassword, oUserAcct, oFolder
Dim sGroupList, iTemp, oGroup
‘ Define the base path in which to create the home directories.
Dim sHomePath, sMsg
sHomePath = « \\iridis1\c$\users\ »
‘ Go through the record set one row at a time.
Do Until oRS.EOF
‘ Get the user information from this row.
sUserID = oRS(« UserID »)
sFullName = oRS(« FullName »)
sDescription = oRS(« Description »)
sHomeDir = oRS(« HomeDirectory »)
sGroups = oRS(« Groups »)
sDialIn = oRS(« DialIn »)
‘ Make up a new password.
sPassword = Left(sUserID,2) & DatePart(« n »,Time) _
& DatePart(« y »,Date) & DatePart(« s »,Time)
‘ Create the user account.
On Error Resume Next
Set oUserAcct = oDomain.Create(« user »,sUserID)
If Err <> 0 Then
sMsg = « An error occurred creating user » _
& sUserID & vbCrLf & vbCrLf
sMsg = sMsg & « The error is: » & Err.Description
MsgBox sMsg
End If
On Error Goto 0
‘ Set account properties.
oUserAcct.SetPassword sPassword
oUserAcct.FullName = sFullName
oUserAcct.Description = sDescription
oUserAcct.HomeDirectory = sHomeDir
‘ Set RAS permission.
If sDialIn = « Y » Then
oUserAcct.RasPermissions = 9
Else
oUserAcct.RasPermissions = 1
End If
‘ Save the account.
oUserAcct.SetInfo
‘ Get a reference to the new account.
‘ This step provides a valid SID and other information.
Set oUserAcct = GetObject(« WinNT:// » & sPDC & « / » & sUserID & « ,user »)
‘ Write the password to a file.
oTS.Write sUserID & « , » & sPassword & vbCrLf
‘ Part 4A: Add the user account to groups.
‘ Use the Split function to turn the comma-separated list into an array.
sGroupList = Split(sGroups, « , »)
‘ Go through the array and add the user to each group.
For iTemp = 0 To uBound(sGroupList)
‘ Get the group.
Set oGroup = GetObject(« WinNT:// » & sPDC & « / » & sGroupList(iTemp) _
& « ,group »)
‘ Add the user account.
oGroup.Add oUserAcct.ADsPath
‘ Release the group.
Set oGroup = Nothing
Next
‘ Part 4B: Create the user s home directory.
‘ (Append the UserID to the Home Path variable).
Set oFolder = oFSO.CreateFolder(sHomePath & sUserID)
‘ Part 5: Release the user account.
Set oUserAcct = Nothing
‘ Move to the next row in the record set.
oRS.MoveNext
Loop
‘ Part 6: Final clean up and close down.
oRS.Close
oTS.Close
WScript.Echo « Passwords have been written » & « to C:\passwords.txt. »
Téléchargez cette ressource
![Guide inmac wstore pour l’équipement IT de l’entreprise](https://www.itpro.fr/wp-content/uploads/2024/04/Guide-inmac-wstore-Juillet-2024.jpg)
Guide inmac wstore pour l’équipement IT de l’entreprise
Découvrez les dernières tendances et solutions IT autour des univers de Poste de travail, Affichage et Collaboration, Impression et Infrastructure, et notre nouveau dossier thématique sur l’éco-conception et les bonnes pratiques à adopter pour réduire votre impact environnemental.