VBScript Adding Users to ADSI

By chrystal.nunnold ·
I am new to using VBScript. I have been teaching myselft for about 3 weeks. I am trying to create a script that will read from an excel spreadsheet and create the users, plug in their information into ADUC, mail enable them, add them to groups, create the home drive, and assign the permissions.

Below is the script I have written so far. I am having troubles with the File Permissions on the home drive. I do not get an error but the user is never added to the security tab of the folder. I am able to get the permissions added by using a batch file, but I would perfer it to work with the script.

I am also unable to get the user to be added to any groups. I am getting the following error on it.
Line: 100
Char: 1
Error: 0x80005000
Code: 80005000
Source: (Null)

Here is the script so far. I have not added the exchange pieces yet.

Option Explicit

Dim objRootLDAP, objContainer, objUser, objShell
Dim objExcel, objSpread, intRow, intCol
Dim strUser, strOU, strSheet
Dim strCN, strSam, strFirst, strLast, strPWD
Dim strdescription, strUPN, strHomeFolder, strHomeDrive, strHomeFolderadm
Dim strLogonScript, strExtensionAttribute1, strmail
Dim objFSO, objFolder, objFileShare, strDest
Dim strGroupproxy_user, strGroupDNKoch_Emp, strGroupDNDLICTMBX01

' -------------------------------------------------------------'

' Important change OU= and strSheet to reflect your domain

' -------------------------------------------------------------'
strOU = " ," ' Note the comma
strSheet = "path to spreadsheet"'given

' Bind to Active Directory, Users container.
Set objRootLDAP = GetObject("LDAP://rootDSE")
Set objContainer = GetObject("LDAP://" & strOU & _

' Open the Excel spreadsheet
Set objExcel = CreateObject("Excel.Application")
Set objSpread = objExcel.Workbooks.Open(strSheet)
intRow = 3 'Row 1 often contains headings

' Here is the 'DO...Loop' that cycles through the cells

' Note intRow, x must correspond to the column in strSheet

Do Until objExcel.Cells(intRow,1).Value = ""
strCN = Trim(objExcel.Cells(intRow, 1).Value)
strSam = Trim(objExcel.Cells(intRow, 2).Value)
strFirst = Trim(objExcel.Cells(intRow, 3).Value)
strLast = Trim(objExcel.Cells(intRow, 4).Value)
strPWD = Trim(objExcel.Cells(intRow, 5).Value)
strdescription = Trim(objExcel.Cells(intRow, .Value)
strUPN = Trim(objExcel.Cells(intRow, 10).Value)
strHomeFolder = "\\virtualServername\" & strsam
strHomeDrive = "Z:"
strHomeFolderadm = "\\actualpath to server\" & strsam
strLogonScript = Trim(objExcel.Cells(intRow, 11).Value)
strExtensionAttribute1 = Trim(objExcel.Cells(intRow, 7).Value)
strmail = Trim(objExcel.Cells(intRow, 14).Value)

'Build the actual User from data in strSheet.
Set objUser = objContainer.Create("User", "cn=" & strCN)
objUser.sAMAccountName = strSam
objUser.givenName = strFirst = strLast
objuser.description = strdescription
objUser.userPrincipalName = strUPN
objUser.homeDirectory = strHomeFolder
objUser.homeDrive = strHomeDrive
objUser.scriptPath = strLogonScript
objUser.extensionAttribute1 = strExtensionAttribute1
objuser.mail = strmail

' Separate section to enable account with its password
objUser.userAccountControl = 512
objUser.pwdLastSet = 0
objUser.SetPassword strPWD

'This Section will Create The Users Home Drive on \\actual path to server
Set ObjShell = CreateObject("Wscript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.CreateFolder("\\actual path to server\" & strSam)
wscript.echo(strsam & " homedrive was created.")

'Assigning File Permissions
If objFSO.FolderExists(strHomeFolder) Then
' Assign user permission to home folder.
intRunError = objShell.Run("%COMSPEC% /c Echo Y| cacls "_
& strHomeFolderadm & " /t /c /g Administrators:f "_
& strsam & ":C", 2, True)
If intRunError <> 0 Then
Wscript.Echo "Error assigning permissions for user " _
& strsam & " to home folder " & strHomeFolderadm
End If
End If

'Group Administration

strUser = "CN=" & strsam
strGroupproxy_user = "distinguished name of group1,"
strGroupDNK_Emp = "distinguished name of group2,"
strGroupDNDLICTMBX01 = "distinguished name of group3,"

' Add (str)User to (str)Group
Set objGroup = GetObject("LDAP://"& strGroupproxy_user _
& strOU)

WScript.Echo "Check " & strOU & " for " & strGroup & " = " & strUser

' Increment to next user.
intRow = intRow + 1

Wscript.Echo "Done"

Please if you can provide any advice that would be greatly appreciated.

This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Answers

Collapse -

Add to a Group

by dlwhite In reply to VBScript Adding Users to ...

strGroup = "CTX-Baseapps"
strDomainGroupsOU = "CN=Users"
strNamingContext = "DC=domain,DC=com"
Set objGroup = GetObject("LDAP://cn=" & strGroup & "," & strDomainGroupsOU & "," & strNamingContext)
objGroup.Put "sAMAccountName", strGroup
objGroup.Add objUser.ADSPath

Related Discussions

Related Forums