PDA

View Full Version : Add mutiple users to the active directory, with homemap and userrights (All)



Orcie
04-05-2004, 02:53 AM
Hi,

I'm writing a script that has to add users to the active directory with a homemap and permissions.
The script I have so far only works to add 1 user, can anyone help me out to let it add mutiple users by using a Excel spreadsheet or something.

Tnx on avance for the help.

Orcie

Here's the script I have so far:
======================================
Option Explicit

Const WAIT_ON_RETURN = True
Const HIDE_WINDOW = 0
Const USER_ROOT_UNC = "\\testserver\users$\" 'Set Home Folder Location Here

Dim WshShell, WshNetwork, objFS, objOU, objServer, objShare, usr, struser

Set WshShell = Wscript.CreateObject("Wscript.Shell")
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objOU = GetObject("LDAP://CN = users,DC = wshserver,DC = lokaal")

'Create the User
Set usr = objou.Create("user", "CN=Testgebruiker02")
usr.Put "samAccountName", "Testgebuiker02"
usr.Put "sn", "Testgebruiker02"
usr.Put "givenName", "Testgebuiker02"
usr.Put "userPrincipalName", "Testgebruiker02@wshserver.lokaal"
usr.Put "telephoneNumber", "(456) 456 4572"
usr.Put "title", "Sir"
usr.SetInfo

'Now that the user is created, reset the user's password and enable its account.

usr.SetPassword("changeme")
usr.AccountDisabled = False
usr.SetInfo

'Now create the User's Home Folder and set permissions.
strUser = usr.samAccountName
Call objFS.CreateFolder(USER_ROOT_UNC & strUser)
Call WshShell.Run("cacls " & USER_ROOT_UNC & strUser & _
" /e /g Administrators:F", HIDE_WINDOW, WAIT_ON_RETURN)
Call WshShell.Run("cacls " & USER_ROOT_UNC & strUser & _
" /e /g " & strUser & ":C", HIDE_WINDOW, WAIT_ON_RETURN)
======================================

Belaflek
04-12-2004, 08:05 AM
I [censored] with Excel object model
I usually get a txt file and read each line using the FSO object

Orcie
04-19-2004, 07:05 AM
Ok I found this script to add several users to te AD via a TXT file. I had to edit it a bit to make it working for me. What code do I have to add now to also make a homemap for those users?

=================================
'source where I found the script: http://www.nwc.com/shared/article/printFullArticle.jhtml?articleID=15000742

Option Explicit
Const ForReading = 1

'Script to create mutiple user accounts from a csv list:
Const vbMinimizedNoFocus = 6
Const XFFFF = &H255
Const ADS_DONT_EXPIRE_PASSWD = &H10000
Const ADS_ACETYPE_ACCESS_DENIED_OBJECT = &H6
Const ADS_ACEFLAG_OBJECT_TYPE_PRESENT = &H1
Const CHANGE_PASSWORD_GUID = "{ab721a53-1e2f-11d0-9819-00aa0040529b}"
Const ADS_RIGHT_DS_CONTROL_ACCESS = &H100

Dim mydomain
Dim user_ou
Dim group_dn
Dim user_dn
Dim adsDomain, adsUser, fso, tsInputfile, strLine, arrInput
Dim objHash, Key, interUserflags, objSD, objDACL, objACE, arrTrustees, strTrustee
Dim wshShell, Group, Newgroup, NewGroupLen, ProfileName

mydomain = "Dc=citrixadmin,DC=citrix,DC=be"
user_ou = "cn=Users," & mydomain
group_dn = "LDAP://" & Newgroup & "," & user_ou

Set adsDomain = GetObject("LDAP://cn = Users,DC = citrix,dc = be")
Set fso = CreateObject("Scripting.FileSystemObject")

'Opens the textfile for readig; no new file if export.txt does not exist:
Set tsInputfile = fso.OpenTextFile("c:\export.txt", ForReading, False)

'Excute the loop to build new users:
While Not tsInputFile.AtEndOfStream

strline = tsInputFile.Readline
arrInput = Split(strLine,",")

set adsUser = adsDomain.Create("User","cn=" & arrInput(0))
adsUser.Put "sAMAccountName", arrInput(0)
adsUser.Put "userPrincipalName", arrInput(0) & "@wshserver.lokaal"

'Write the new user object from the property cache:
adsUser.SetInfo

'Set the Password:
adsUser.SetPassword (arrInput(0))
adsUser.SetPassword "gccc"

'Set the properties:
adsUser.AccountDisabled = False
adsUser.Description = "User created by Script"
adsUSer.IsAccountLocked = False
ProfileName = arrInput(0)
adsUser.Profile = "\\citrixadmin\users$\" & (arrInput(0))
adsUser.Passwordrequired = True
adsUser.DisplayName = ArrInput(1)

'Set all the properties for the user; read back the data, including defaults:
adsUser.SetInfo
adsUser.GetInfo

'Make sure the password never changes and that the user cannot change it:
DIM intUserflags

intUserflags = adsUser.Get("UserAccountControl")
intUserFlags = intUserFlags Or ADS_DONT_EXPIRE_PASSWD

adsUser.Put "UseraccountControl", intUserFlags

Set objSD = adsUser.Get ("ntSecurityDescriptor")
Set objDACL = objSD.DiscretionaryAcl
arrTrustees = array ("net authority\self", "Everyone")

For Each strTrustee in arrTrustees
set objACE = CreateObject("AccessControlEntry")

objACE.Trustee = strTrustee
objACE.AceFlags = 0
objACE.AceType = ADS_ACETYPE_ACCESS_DENIED_OBJECT
objACE.Flags = ADS_ACEFLAG_OBJECT_TYPE_PRESENT
objACE.ObjectType = CHANGE_PASSWORD_GUID
objACE.AccessMask = ADS_RIGHT_DS_CONTROL_ACCESS
objDACL.AddAce objACE
next

'objSD.DiscretionaryAcl = objDACL
'adsUser.Put "nTSecurityDescriptor", objSD
'adsUser.SetInfo

'Set the workstations:
'adsUser.put "userWorkstations", (arrInput(7)) & "," (arrInput(8)) & "," (arrInput(9))
adsUser.SetInfo
Set Group = nothing
Set adsUser = nothing
Wend

'At end of loop, close the file:
tsInputFile.Close
=================================