Script that will add multiple accounts to a AD group

Does anyone have a simple script that will add multiple (300+) accounts to an ad group?  I need this asap!

I appreciate it.
LVL 1
mystikal1000Asked:
Who is Participating?
 
RobSampsonConnect With a Mentor Commented:
Hi, see if this VBScript will do the job for you.  I haven't tested it yet.  Add usernames to users.txt and specify your group in the Set objGroup line, and see how it goes.

Regards,

Rob.

Set objGroup = GetObject("CN=GroupName,OU=GroupOU,DC=domain,DC=com")
strUsersFile = "users.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1

Set objUsers = objFSO.OpenTextFile(strUsersFile, intForReading, False)
strAllDNs = ""
While Not objUsers.AtEndOfStream
	strUserName = objUsers.ReadLine
	strUserDN = ""
	strUserDN = Get_LDAP_User_Properties("user", "samAccountName", strUserName, "distinguishedName")
	If strUserDN <> "" Then
		If strAllDNs = "" Then
			strAllDNs = strUserDN
		Else
			strAllDNs = strAllDNs & "|" & strUserDN
		End If
	Else
		WScript.Echo "Could not find " & strUserName & ". User will not be added to new groups."
	End If
Wend
objUsers.Close
arrAllDNs = Split(strAllDNs, "|")

On Error GoTo 0	
objGroup.PutEx ADS_PROPERTY_APPEND, "member", arrAllDNs 
objGroup.SetInfo
If Err.Number = 0 Then
	WScript.Echo "Users have been added."
Else
	WScript.Echo "Error " & Err.Number & ": " & Err.Description
End If
Set objGroup = Nothing

MsgBox "Done"

Function Get_LDAP_User_Properties(strObjectType, strSearchField, strObjectToGet, strCommaDelimProps)
	
	If InStr(strObjectToGet, "\") > 0 Then
		arrGroupBits = Split(strObjectToGet, "\")
		strDC = arrGroupBits(0)
		strDNSDomain = strDC & "/" & "DC=" & Replace(Mid(strDC, InStr(strDC, ".") + 1), ".", ",DC=")
		strObjectToGet = arrGroupBits(1)
	Else
		Set objRootDSE = GetObject("LDAP://RootDSE")
		strDNSDomain = objRootDSE.Get("defaultNamingContext")
	End If

	strBase = "<LDAP://" & strDNSDomain & ">"
	' Setup ADO objects.
	Set adoCommand = CreateObject("ADODB.Command")
	Set adoConnection = CreateObject("ADODB.Connection")
	adoConnection.Provider = "ADsDSOObject"
	adoConnection.Open "Active Directory Provider"
	adoCommand.ActiveConnection = adoConnection

 
	' Filter on user objects.
	'strFilter = "(&(objectCategory=person)(objectClass=user))"
	strFilter = "(&(objectClass=" & strObjectType & ")(" & strSearchField & "=" & strObjectToGet & "))"

	' Comma delimited list of attribute values to retrieve.
	strAttributes = strCommaDelimProps
	arrProperties = Split(strCommaDelimProps, ",")

	' Construct the LDAP syntax query.
	strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
	adoCommand.CommandText = strQuery
	adoCommand.Properties("Page Size") = 100
	adoCommand.Properties("Timeout") = 30
	adoCommand.Properties("Cache Results") = False

	WScript.Echo ""
	WScript.Echo "Executing " & strQuery
	' Run the query.
	Set adoRecordset = adoCommand.Execute
	' Enumerate the resulting recordset.
	Do Until adoRecordset.EOF
	    ' Retrieve values and display.    
	    For intCount = LBound(arrProperties) To UBound(arrProperties)
	    	If strDetails = "" Then
	    		strDetails = adoRecordset.Fields(intCount).Value
	    	Else
	    		strDetails = strDetails & VbCrLf & adoRecordset.Fields(intCount).Value
	    	End If
	    Next
	    ' Move to the next record in the recordset.
	    adoRecordset.MoveNext
	Loop

	' Clean up.
	adoRecordset.Close
	adoConnection.Close
	Get_LDAP_User_Properties = strDetails

End Function 

Open in new window

0
 
Stelian StanNetwork AdministratorCommented:
With some tweaking this should work: http://www.hiperlogic.com/blog/?p=788
0
 
mystikal1000Author Commented:
I rather have a vbs script than powershell.  :)
0
 
Stelian StanNetwork AdministratorCommented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.