'// Name: bulkadduser1.vbs '// Purpose: Create user script from CSV file '// Notes: '// 1. Input file must be comma delimited '// 2. Fields must be in the following order and must not be left blank: '// First Name, Last Name, Password, Group Name, Child OU 1, Child OU 2, Child OU 3, '// SAM Logon, Home Drive, Logon Script, Description, Home Directory '// 3. Must run stupermissions.bat AFTER this script to set permissions on home folders '// 4. Must be member of Domain Admins group to run '// 5. OU's and groups must have been created prior to running this script '// 6. Home Directory must use full USN path, i.e. SERVERSHAREusername -- system variables are not allowed '// 7. Must change InpFile and batFile to correct drive and path prior to running script '// Define variables Option Explicit Dim oFSO, oTF, oOU, oUser, oGroup, oRoot, oFolder, oPerm, oShell, oExec Dim sCN, sOU1, sOU2, sOU3 Dim aLine, sLine, sLogon, sPass, sGroup, sRoot, sDN, vFlg, sPath Dim sFname, sLName, sHomedrive, sLogscript, sDescription, sHomeDir '// Set file names -- replace these with desired files Const InpFile = "C:stuimport.csv" 'Import file Const batFile = "C:stupermissions.bat" 'batch file used for resetting home dir permissions Const ForReading = 1 Const ForWriting = 2 Set oFSO = CreateObject("Scripting.FileSystemObject") Set oTF = oFSO.OpenTextFile(InpFile,ForReading,True) Set oPerm = oFSO.OpenTextFile(batFile,ForWriting,True) Set oShell = CreateObject("WScript.Shell") '// vFlg verifies that required fields are in input file vFlg = True Do While oTF.AtEndOfStream <> True sLine = oTF.ReadLine aLine = split(sline, ",",-1,1) sFname = aLine(0) sLname = aLine(1) '// Sets container name to Last Name, First Name sCN = sLname &", "&sFname sPass = aLine(2) sGroup = aLine(3) sOU1 = aLine(4) sOU2 = aLine(5) sOU3 = aLine(6) sLogon = aLine(7) sHomedrive = aLine(8) sLogscript = aLine(9) sDescription = aLine(10) sHomeDir = aLine(11) sPath = aLine(12) If vFlg = True Then If isEmpty(sGroup) Or isEmpty(sOU3) Or isEmpty(sOU2) Or isEmpty(sOU1) Then msgbox "Missing Parameter" & vbCr & _ "First Line must contain:" & vbCr & _ "First Name, Last Name, Password, Group Name, Child OU, Child OU, Root OU," & vbCr & _ "SAM Logon ID, Home Drive, Logon Script, Description, Home Directory",_ vbExclamation, "Add Bulk Users" wscript.Quit Else vFlg = False End If End If '// Call the Create User routine CreateaUser '// Call the add to Group routine Add2Group(sDN) Loop oPerm.Close Msgbox "Script complete." & vbCr & _ "Run stupermissions.bat to set proper permissions on Home folders" _ ,vbInformation, "Add Bulk Users" Set oTF = Nothing Set oFSO = Nothing Set oUser = Nothing Set oGroup = Nothing Set oOU = Nothing Set oRoot = Nothing '//-------------------------- '// Create User subroutine '//-------------------------- Sub CreateaUser() '// Bind to the domain Root Set oRoot = GetObject("LDAP://rootDSE") sRoot = oRoot.Get("defaultNamingContext") '// Bind to the OU where users are to be added Set oOU = GetObject("LDAP://ou=" & sOU1 & ",ou=" & sOU2 & ",ou=" & sOU3 & "," & sRoot) '// Remove comment from following line to enable screen display for debug '// msgbox sCN & "ou=" & sOU1 & ",ou=" & sOU2 & ",ou=" & sOU3 & "," & sRoot Set oUser = oOU.Create("user", "cn=" & sCN) '// Remove comment from following line to enable error check from Create operation for debug '// If err.number<>0 then msgbox err.number On Error Resume Next '// Load fields in AD record oUser.put "sAMAccountName", lcase(sLogon) oUser.put "givenName", sFname oUser.put "sn", sLname oUser.put "UserPrincipalName", lcase(sLogon) oUser.put "DisplayName", sLname &", "&sFname oUser.put "name", sCN oUser.put "homeDrive", sHomedrive oUser.put "scriptPath", sLogscript oUser.put "description", sDescription oUser.put "homeDirectory", sHomeDir oUser.put "profilePath", sPath oUser.SetInfo '// Test for duplicate User If err.number = -2147019886 then msgbox "User logon for " & sLogon & " already exists." _ ,vbExclamation, "Add Bulk Users" Exit Sub End If '// Set initial password oUser.setpassword sPass oUser.AccountDisabled = False oUser.SetInfo '// Create user home folder Set oFolder = oFSO.CreateFolder(sHomeDir) '// Write line to bat file for cacls command to reset ACL on user home dir oPerm.WriteLine("echo y|cacls "& sHomeDir &" /G "& lcase(sLogon) &":C Administrators:F Staff:C") '// Set the User's DN for adding to group sDN = oUser.get("DistinguishedName") End Sub '//-------------------------- '// Add to Group subroutine '//-------------------------- Sub Add2Group(Byval sDN) Const ADS_PROPERTY_APPEND = 3 On Error Resume Next '// Test if group is empty If IsEmpty(oGroup) Then Set oGroup = GetObject _ ("LDAP://cn=" & sGroup & ",ou=" & sOU1 & ",ou=" & sOU2 & ",ou=" & sOU3 & "," & sRoot) End If '// Add user to group oGroup.PutEx ADS_PROPERTY_APPEND, _ "member", Array(sDN) oGroup.SetInfo sDN = Nothing '// Test if user is already member of group If err.number <> 0 Then msgbox "User " & sLogon & _ " is already a member of " & sGroup, _ ,vbExclamation, "Add Bulk Users" Exit Sub End If End Sub