Question
Thread display: Collapse - |
All Answers
Start or search
Create a new discussion
If you're asking for technical help, please be sure to include all your system info, including operating system, model number, and any other specifics related to the problem. Also please exercise your best judgment when posting in the forums--revealing personal information such as your e-mail address, telephone number, and address is not recommended.
VBScript Adding Users to ADSI
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 = "domain.com/OU=Users ," ' Note the comma
strSheet = "path to spreadsheet"'given
' Bind to Active Directory, Users container.
Set objRootLDAP = GetObject("LDAP://rootDSE")
Set objContainer = GetObject("LDAP://" & strOU & _
objRootLDAP.Get("defaultNamingContext"))
' 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,
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
objUser.sn = strLast
objuser.description = strdescription
objUser.userPrincipalName = strUPN
objUser.homeDirectory = strHomeFolder
objUser.homeDrive = strHomeDrive
objUser.scriptPath = strLogonScript
objUser.extensionAttribute1 = strExtensionAttribute1
objuser.mail = strmail
objUser.SetInfo
' Separate section to enable account with its password
objUser.userAccountControl = 512
objUser.pwdLastSet = 0
objUser.SetPassword strPWD
objUser.SetInfo
'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)
objGroup.add(objUser.ADsPath)
WScript.Echo "Check " & strOU & " for " & strGroup & " = " & strUser
' Increment to next user.
intRow = intRow + 1
Loop
Wscript.Echo "Done"
objExcel.Quit
WScript.Quit
Please if you can provide any advice that would be greatly appreciated.