RE: Help With Bulk User Creation Script

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Excellent link Sam...lots of good resources there. Thanks!

"Sam" wrote:

AJ,

Have a look at Richard Mueller's site, he has a script that already does
what you want.

http://www.rlmueller.net/CreateUsers.htm


--
Regards,
Sam Gaw


"Aj" wrote:

Hi I am trying a Script for creating Bulk User : Its giving error on line 105
. . Please let me know what is missing in this . . Enviorment is Windows 2003
Domain and I am doing this on the DC


'// 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. \\SERVER\SHARE\username
-- 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
Dim sFname, sLName, sHomedrive, sLogscript, sDescription, sHomeDir


'24// Set file names -- replace these with desired files

Const InpFile = "c:\temp.csv" 'Import file
Const batFile = "C:\perm.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")


'37// 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)
'line 45 // 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)

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

'71// 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

'94//--------------------------
'// Create User subroutine
'//--------------------------

Sub CreateaUser()

'100// 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 & "," & sRoot) '//& ",ou=" & sOU2 &
",ou=" & sOU3

'// 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.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) &":F
Administrators:F")

'// 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




.



Relevant Pages

  • Help With Bulk User Creation Script
    ... Hi I am trying a Script for creating Bulk User: Its giving error on line 105 ... Dim sFname, sLName, sHomedrive, sLogscript, sDescription, sHomeDir ... ,vbInformation, "Add Bulk Users" ... Sub CreateaUser() ...
    (microsoft.public.windows.server.scripting)
  • RE: Help With Bulk User Creation Script
    ... Have a look at Richard Mueller's site, he has a script that already does ... Dim sFname, sLName, sHomedrive, sLogscript, sDescription, sHomeDir ... '37// vFlg verifies that required fields are in input file ... Sub CreateaUser() ...
    (microsoft.public.windows.server.scripting)
  • Re: Document contains no data and httpd signal Bus error
    ... sam wrote: ... > Ijust pass running the test.php script by inputing imap/notls in the ... > PHP IMAP Support Test ...
    (comp.unix.bsd.freebsd.misc)
  • Re: Calling HTML JScript from C# app
    ... not quite behave the same in a hosted environment vis-a-vis in IE. ... any chance you know how to call C# from script? ... "Sam Martin" wrote: ... >> just can't see how to call a jscript function. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: [9fans] Non-parallel loop in Sam
    ... sam, I would simply enter the following sam commands: ... maybe a slight change would be needed on Plan 9). ... You could then merely invoke that script for whatever "dot" ... Sometimes it is useful to enter nroff ...
    (comp.os.plan9)