?
Solved

How do I script bulk creation of AD groups?

Posted on 2011-10-18
8
Medium Priority
?
464 Views
Last Modified: 2012-05-12
Points of My Scenario:
1. I am admin of a Windows Server 2003 domain
2. I need to create 96 (ninety-six) domain local groups
3. I have a text file containing the group names - one group per line
QUESTION: How can I script the creation of these 96 groups into an OU, using the text file?
0
Comment
Question by:waltforbes
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 1

Assisted Solution

by:mariofxp
mariofxp earned 600 total points
ID: 36989488
there are two solutions:
1. Use dsadd (see http://technet.microsoft.com/en-us/library/cc754037(WS.10).aspx)
2. Use powershell and the quest cmdlets provided with free activeroles (eg. New-qadgroup)

in the 1st solution you'll have to insert dsadd in a cmd.exe for statement (see http://ss64.com/nt/for_f.html)
in the 2nd solution you do a pipe with the result of the cat <filename> command and the new-qadgroul command
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 36989714
Hi, you can try this.

Regards,

Rob.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDomain = objRootDSE.Get("defaultNamingContext")

' OU to create groups in
strOU = "CN=Users," & strDomain
' File to read group names from
strGroupsFile = "groups.txt"
' File to record results to
strResultsFile = "results.txt"

Const ADS_GROUP_TYPE_LOCAL_GROUP = &h4
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
Set objResults = objFSO.CreateTextFile(strResultsFile, True)
objResults.WriteLine "Creating Groups " & Now
Set objGroups = objFSO.OpenTextFile(strGroupsFile, intForReading, False)
While Not objGroups.AtEndOfStream
	strGroupName = objGroups.ReadLine
	Set objGroup = objOU.Create("Group", "cn=" & strGroupName)
	objGroup.Put "sAMAccountName", strGroupName
	objGroup.Put "groupType", ADS_GROUP_TYPE_LOCAL_GROUP
	On Error Resume Next
	objGroup.SetInfo
	If Err.Number <> 0 Then
		objResults.WriteLine strGroupName & " already exists."
		Err.Clear
		On Error GoTo 0
	Else
		On Error GoTo 0	
		objResults.WriteLine strGroupName & " created."
	End If
	Err.Clear
	On Error GoTo 0
Wend
objGroups.Close
objResults.Close

WScript.Echo "Done. Please see " & strResultsFile

Open in new window

0
 
LVL 39

Expert Comment

by:Krzysztof Pytko
ID: 36990820
Hi, if you wish ti try other methods, like Quest PowerShell module for AD or Microsoft DS Tools, please let me know. I would help you then. If not, follow with Rob's syntax :)

Regards,
Krzysztof
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:waltforbes
ID: 36993175
To Rob: please note the following error I receive:
C:\Scripts\Create_Groups_from_File.vbs(19, 2) Microsoft VBScript runtime error:
Object required: 'objOU'

To iSiek: I am interesed in Quest PowerShell module for AD.
0
 
LVL 39

Expert Comment

by:Krzysztof Pytko
ID: 36993229
OK, so try this syntax

Get-Content c:\groups.txt | %{ New-QADGroup -name "$_" -ParentContainer "ou=GroupLocation,dc=domain,dc=local" -DisplayName "$_" -GroupType "Security" -GroupScope "DomainLocal" -SamAccountName "$_"

Krzysztof
0
 
LVL 39

Assisted Solution

by:Krzysztof Pytko
Krzysztof Pytko earned 680 total points
ID: 36993238
sorry, missed the last charecter in a syntax, so I will repeat this

Get-Content c:\groups.txt | %{ New-QADGroup -name "$_" -ParentContainer "ou=GroupLocation,dc=domain,dc=local" -DisplayName "$_" -GroupType "Security" -GroupScope "DomainLocal" -SamAccountName "$_" }
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 720 total points
ID: 36996308
Sorry, my mistake....forgot to bind to the target OU...

Rob.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDomain = objRootDSE.Get("defaultNamingContext")

' OU to create groups in
strOU = "CN=Users," & strDomain
' File to read group names from
strGroupsFile = "groups.txt"
' File to record results to
strResultsFile = "results.txt"

Const ADS_GROUP_TYPE_LOCAL_GROUP = &h4
Set objOU = GetObject("LDAP://" & strOU)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
Set objResults = objFSO.CreateTextFile(strResultsFile, True)
objResults.WriteLine "Creating Groups " & Now
Set objGroups = objFSO.OpenTextFile(strGroupsFile, intForReading, False)
While Not objGroups.AtEndOfStream
	strGroupName = objGroups.ReadLine
	Set objGroup = objOU.Create("Group", "cn=" & strGroupName)
	objGroup.Put "sAMAccountName", strGroupName
	objGroup.Put "groupType", ADS_GROUP_TYPE_LOCAL_GROUP
	On Error Resume Next
	objGroup.SetInfo
	If Err.Number <> 0 Then
		objResults.WriteLine strGroupName & " already exists."
		Err.Clear
		On Error GoTo 0
	Else
		On Error GoTo 0	
		objResults.WriteLine strGroupName & " created."
	End If
	Err.Clear
	On Error GoTo 0
Wend
objGroups.Close
objResults.Close

WScript.Echo "Done. Please see " & strResultsFile

Open in new window

0
 

Author Closing Comment

by:waltforbes
ID: 37009585
I'm so happy!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Let's recap what we learned from yesterday's Skyport Systems webinar.
Transferring FSMO roles is done when an admin wants to split roles between certain Domain Controllers or the Domain Controller holding the Roles has been forcefully demoted using dcpromo / forceremoval
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

807 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