This blog is by design...

Friday, January 06, 2006

Add users to a group from a CSV file

Here's another that I'm posting per request. This one comes in handy for me quite often. It can be modified to read in (from the file) samAccountnames, the smtp address, just about anything that can be converted using the name translate method. Here's a link that talks about some of the things you can do with IADsNameTranslate. Read through the script to see what attributes you need to modify to get it to work for you. Enjoy!

'------------------------------------------------------
'Use any and all scripts from this site at your own risk
'Test test test!
'This script will take a csv file of samAccountnames (ntname)
'and add all the users to the specified group.

'By KLW, 08/26/05

On Error Resume Next

Const ADS_PROPERTY_CLEAR = 1
Const ADS_PROPERTY_UPDATE = 2
Const ADS_PROPERTY_APPEND = 3
Const ADS_PROPERTY_DELETE = 4

Const ADS_NAME_INITTYPE_GC = 3

Const ADS_NAME_TYPE_USER_PRINCIPAL_NAME = 9
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1 'Distinguished Name

'----------------------------------------------------------
'Replace the below item with the security group
'or dist list you want modified
'Modify the dc=microsoft,dc=com to match your domain
Const strGroup = "LDAP://CN=IT Dept,OU=Distribution " _
& "Lists,OU=Exchange,DC=microsoft,DC=com"
Set objGroup = GetObject(strGroup)

'Replace this item with the list of users
Const strFile = "c:\users.csv"

'---------------------------------------------------------
set objReadFSO = CreateObject ("Scripting.FileSystemObject")
set objUsersFile = objReadFSO.OpenTextFile(strFile)

'---------------------------------------------------------
'Determine DNS Domain Name
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")

'--------------------------------------------------------
' Use the NameTranslate object to find the NetBIOS
' domain name from the DNS domain name.
Set objTrans = CreateObject("NameTranslate")
objTrans.Init ADS_NAME_INITTYPE_GC, strDNSDomain
objTrans.Set ADS_NAME_TYPE_1779, strDNSDomain
strNetBIOSDomain = objTrans.Get(ADS_NAME_TYPE_NT4)

'The following removes the \ as in DOMAIN\
strNetBIOSDomain = Left(strNetBIOSDomain, Len(strNetBIOSDomain) - 1)
'wscript.Echo strNetBiosDomain

'------------------------------------------------------
do while objUsersFile.AtEndOfStream = False
strUser = rtrim(Ucase(objUsersFile.readLine))

objTrans.Init ADS_NAME_TYPE_1779, strNetBIOSDomain
objTrans.Set ADS_NAME_TYPE_NT4, strNetBIOSDomain & "\" & strUser
strUserDN = objTrans.Get(ADS_NAME_TYPE_1779)

'wscript.Echo strUserDN
set objUser = GetObject("LDAP://"& strUserDN)
strDNPath = objUser.distinguishedName

objGroup.PutEx ADS_PROPERTY_APPEND, "member", Array(strDNPath)
objGroup.SetInfo
Loop

'----------------------------------------------------
wscript.echo "All Done"

set objUser = Nothing
set objGroup = Nothing
set objUsersFile = Nothing
set objReadFSO = Nothing
set objRootDSE = Nothing
set objTrans = Nothing

2 Comments:

Post a Comment

<< Home