WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Samples from Users

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Create User in AD with CSV file


Question:

Does anyone have a program, that can read a csv-file and automaticlly puts teh users into the AD???

Answer:

Here is a script I wrote to create support accounts in our AD. I revised it a bit to remove our specific requirements. I left some of it commented out so that if you need to do similar stuff you can. You will need to change some variables for sDC and sDomain. Let me know if this helps.
;******************************************************************************
;          		Program Information
;******************************************************************************
; CreateAccounts.wbt
; Created by Brad Sjue 
; Date: November 21, 2003 
; This script will create accounts in AD from a CSV file

; This script uses a csv file for it's input.  Use the following format for the CSV
; Barrett Brantley,bbrantle
; Where the first column is the full username and the second column is the userid. 

;Optional Debugging
If IsKeyDown(@SHIFT || @CTRL)    
	sTrcFile = Strcat(Environment("TEMP"),"\~AcctCreation.trc")
	If FileExist(sTrcFile) Then FileDelete(sTrcFile)
	DebugTrace(@ON,sTrcFile)
Endif

;******************************************************************************
;          		Subroutines and Functions
;******************************************************************************
;==============================================================================
;* 			UDFWRITELOG				
;==============================================================================
; This function will write out the Log file and a message with the date prepended. 
#DefineFunction UDFWriteLog(LogFile, LogText)
	CurrDate = TimeDate ( )
	handle = FileOpen(LogFile, "APPEND")
	Logme = Strcat(CurrDate, @TAB, @TAB, LogText)
	FileWrite(handle, Logme)
	FileClose(handle)
	Return
#EndFunction ;UDFWriteLog

;==============================================================================
;* 			CREATEADUSER				
;==============================================================================
#DefineSubroutine CreateADUser(sAdsiPath, NewUser, AcctDesct, FirstName, LastName)

	sDisplayName = StrCat(FirstName, ' ', LastName)
	sFilter = StrCat("sAMAccountName=", NewUser)
	
	MyOuPaths = dsFindPath(sNewOUUsersAdsiPath, sFilter)
	If MyOuPaths != ''
		BoxText('%sDisplayName% already exists')
; 		Display(1, "Client User Creation Error!!", "%NewUser% user already exists, OU and User creation will attempt to continue operation")
		Return 'EXISTS' ;Already Exists and return ADSI path
	Endif

	; Create a user object
	sObjectClass = "User"
	sUserName = StrCat("CN=", sDisplayName)	;Changed the user to the display name.   This is what shows in AD in the list.  
	sObjectpath2 = dsCreateObj(sAdsipath, sObjectClass, sUserName)
	
	; Set the manditory properities.
	dsSetProperty(sObjectPath2, "samAccountName", NewUser)
	dsSetProperty(sObjectPath2, "DisplayName", sDisplayName)
	dsSetProperty(sObjectPath2, "userPrincipalName", NewUser)
	dsSetProperty(sObjectPath2, "description", AcctDesct)
	dsSetProperty(sObjectPath2, "givenName", FirstName)
	dsSetProperty(sObjectPath2, "sn", LastName)
	dsSetObj(sObjectPath2)

	; Set the password
	password = '123456789@Zd'
	ErrorMode(@OFF)
	dsSetPassword(sObjectPath2, "", password)
	ErrorMode(@CANCEL)
	
	Return sObjectpath2 	;Return the ADSI path of the user for further modifications
	
#EndSubroutine ; CreateADUser


#DefineSubroutine SetTSProps(NewUser, V1, V2, V3, V4, V5, V6, V7, V8)	
;Set the Terminal Server Properties.

	CurrDir = FilePath(IntControl (1004, 0, 0, 0, 0))
	tscmdexe = Strcat(CurrDir, "tscmd.exe")
	 
	PassThis = '/c "%tscmdexe%" %sDC% %NewUser% TimeoutConnection %V1%'
	RunHideWait(Environment('COMSPEC'), PassThis)
		
	PassThis = '/c "%tscmdexe%" %sDC% %NewUser% TimeoutDisconnect %V2%'
	RunHideWait(Environment('COMSPEC'), PassThis)
		
	PassThis = '/c "%tscmdexe%" %sDC% %NewUser% TimeoutIdle %V3%'
	RunHideWait(Environment('COMSPEC'), PassThis)
	
	PassThis = '/c "%tscmdexe%" %sDC% %NewUser% BrokenTimeoutSettings %V4%'
	RunHideWait(Environment('COMSPEC'), PassThis)
	
	PassThis = '/c "%tscmdexe%" %sDC% %NewUser% ReconnectSettings %V5%'
	RunHideWait(Environment('COMSPEC'), PassThis)
	
	PassThis = '/c "%tscmdexe%" %sDC% %NewUser% ShadowingSettings %V6%'
	RunHideWait(Environment('COMSPEC'), PassThis)

	PassThis = '/c "%tscmdexe%" %sDC% %NewUser% AllowLogonTerminalServer %V7%'
	RunHideWait(Environment('COMSPEC'), PassThis)

	PassThis = '/c "%tscmdexe%" %sDC% %NewUser% DeviceClientDrives %V8%'
	RunHideWait(Environment('COMSPEC'), PassThis)
		
#EndSubRoutine ; SetTSProps

#DefineFunction EnableADUser(sAdsiPath)
	UF_ACCOUNTDISABLE = 2
	; Get the current value.
	nValue = dsGetProperty(sAdsiPath, "userAccountControl")
	; Remove the "disable" bit.
	nValue = nValue ^ UF_ACCOUNTDISABLE    ;the caret is a bitwise Exclusive OR (XOR) operator
	; Reset the property.
	dsSetProperty(sAdsiPath, "userAccountControl", nValue)
	Return
#EndFunction ; EnableADUser

;******************************************************************************
;          		Initialize Program
;******************************************************************************
; Add the ADSI Extender
AddExtender("wwads34i.dll")

;Add the Win32 Extender for generating a Group rid - more reliable than ADSI
; This is not needed if not setting a primary group
; AddExtender("WWWNT34i.DLL")

;Define the domain controller
sDC="Your Domain Controller"
sDomain="Your domain ex. yahoo - do not put in the .com!!"

CurrDir = FilePath(IntControl (1004, 0, 0, 0, 0))
AccountLog = Strcat(CurrDir, "Accounts.log")

UDFWriteLog(AccountLog, 'Account Creation Began')

filetypes="CSV Files|*.csv|All Files|*.*"
sUserListFile=AskFileName("Select User List File", CurrDir, filetypes, "", 1)
sUserList = FileOpen(sUserListFile, "READ")
UDFWriteLog(AccountLog, StrCat('User List CSV file is ',sUserListFile))

;******************************************************************************
;          		CREATE Support Accounts
;******************************************************************************

; ; Get the information for the PrimaryGrp
; sPrimGrpPath = "LDAP://%sDC%/CN=PrimGrp,CN=Users,DC=%sDomain%,DC=com" ;Full ADSI Path to PrimGrp
; rid = wntGroupInfo("%sDC%","PrimGrp", @GLOBALGROUP, 2)
; UDFWriteLog(AccountLog, StrCat('PrimGrp Path is ',sPrimGrpPath))

; Set the domain users Group path
sDomUsersGrpPath = "LDAP://%sDC%/CN=Domain Users,CN=Users,DC=%sDomain%,DC=com" ;Full ADSI Path to PrimGrp
UDFWriteLog(AccountLog, StrCat('Domain User Group Path is ',sDomUsersGrpPath))

;Set the Users OU - You can make this what you want.  
sNewOUUsersAdsiPath = "LDAP://%sDC%/CN=Users,DC=%sDomain%,DC=com" 
UDFWriteLog(AccountLog, StrCat('Path Accounts will be created in is ',sNewOUUsersAdsiPath))


while @TRUE             ; Loop till break do us end
    x = FileRead(sUserList)
    If x == "*EOF*" Then Break
    sFullName = StrTrim(ItemExtract (1, x, ","))
    sAccountName = StrTrim(ItemExtract (2, x, ","))
	ParseData (sFullName)
	sFirstName = Param1
	If sFirstName == '' Then sFirstName = 'Unknown'
	If !IsDefined(Param2) Then Param2 = ""
	sLastName = Param2
	If sLastName == '' Then sLastName = 'Unknown'
	sDisplayName=StrCat(sFirstName, " ", sLastName)

	UDFWriteLog(AccountLog, StrCat('User Account is ',sAccountName))
	UDFWriteLog(AccountLog, StrCat('First Name is ',sFirstName))
	UDFWriteLog(AccountLog, StrCat('Last Name is ',sLastName))
	UDFWriteLog(AccountLog, StrCat('Display Name is ',sDisplayName))

	BoxText("Creating the %sAccountName% User")
	AcctDesc = "Account Client User"
	UDFWriteLog(AccountLog, StrCat('Account Description is ',AcctDesc))
	UDFWriteLog(AccountLog, StrCat('Beginning Account Creation for ',sAccountName))
	UserADSIPath = CreateADUser(sNewOUUsersAdsiPath, sAccountName, AcctDesc, sFirstName, sLastName)
	If UserADSIPath == "" Then Message("User Creation Error!!", "Could not determine User ADSI path")
	If UserADSIPath == "EXISTS" 
		UDFWriteLog(AccountLog, StrCat(sAccountName, 'already exists'))
		Continue
	Endif
; 	Enable the User account	
	EnableADUser(UserADSIPath)

; 	This section was used to add a user to a specific group, make it their primary and then remove them from domain users
; 	You will probably not need to do this.  

; 	ErrorMode (@OFF)	;Turning off error mode for now because some of the group checks don't work. 
; 	dsAddToGrp(sPrimGrpPath, UserADSIPath)
; 	dsSetProperty(UserADSIPath, "primaryGroupId", rid)	;Set the primary group to PrimGrp, this is done for all domain users.
; 	dsRemFromGrp(sDomUsersGrpPath, UserADSIPath)	;Remove from the Domain Users Group
; 	UDFWriteLog(AccountLog, StrCat('Group Modifications made for ',sAccountName))
; 	ErrorMode (@CANCEL)
	
; 	This was used to set the Terminal Server Properties for a user it uses a file called
; 	tscmd.exe that sets the Account TS properties. 
; 	UDFWriteLog(AccountLog, StrCat('Setting Terminal Properties for ',sAccountName))
; 	SetTSProps(sAccountName, 720, 1, 30, 1, 0, 2, 1, 0)	

	UDFWriteLog(AccountLog, StrCat('Completed Account Creation for ',sAccountName))

        
EndWhile

FileClose(sUserList)
BoxShut()
UDFWriteLog(AccountLog, 'Account Creation Completed')
Run('notepad.exe', AccountLog)
Message("Support Account Creation Complete", "Completed Creating Support Accounts")

Exit

Article ID:   W15824
File Created: 2004:03:30:15:41:00
Last Updated: 2004:03:30:15:41:00