[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Script that will add multiple accounts to a AD group

Posted on 2012-09-14
5
Medium Priority
?
325 Views
Last Modified: 2012-09-24
Does anyone have a simple script that will add multiple (300+) accounts to an ad group?  I need this asap!

I appreciate it.
0
Comment
Question by:mystikal1000
  • 3
5 Comments
 
LVL 23

Expert Comment

by:Stelian Stan
ID: 38400285
With some tweaking this should work: http://www.hiperlogic.com/blog/?p=788
0
 
LVL 1

Author Comment

by:mystikal1000
ID: 38401692
I rather have a vbs script than powershell.  :)
0
 
LVL 23

Expert Comment

by:Stelian Stan
ID: 38401956
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 2000 total points
ID: 38404020
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question