By David Wiseman (Administrator)Created 30 Mar 2009, Modified 23 Sep 2012
My Rating:
Vote
Rating:
(1 votes)
Views:11801
Downloads:181
Source:

Add all users in OU to Group

Language:  VBScript

Compatibility

Windows XP Unknown Windows 2003 Unknown
Windows 2000 Unknown Windows NT Unknown
Vista Unknown Windows 2008 Yes
Description

Add all the users from a organizational unit (OU) to a group. e.g. Add all users from "Developers" OU to "Developers" group. Could easily be modified to include an additional filter - e.g. Add all users from "Developers" department to "Developers" group.


Notes

This script is designed to run in "CSCRIPT" mode.
e.g.

cscript "MyScriptFile.vbs"

Please change the values in the "Setup" section of the script.  At a minimum, "strRoot", "strGroupName"  and strNETBIOSDomain" would need to be changed to match your own requirements.  You could also change the strFilter to filter for all the users in the "Developers" department instrad of the developers OU. e.g.

strFilter = "(&(objectCategory=person)(objectClass=user)(department=developers))"

 
Code

Line Numbers: On  Off      Plain Text
OPTION EXPLICIT

dim strFilter, strRoot, strScope, strGroupName
dim strNETBIOSDomain, strGroupDN
dim cmd, rs,cn, objGroup
dim objSystemInfo
' ********************* Setup *********************

' Default filter for all user accounts (ammend if required)
strFilter = "(&(objectCategory=person)(objectClass=user))"
' scope of search (default is subtree - search all child OUs)
strScope = "subtree"

' search root. e.g. ou=MyUsers,dc=wisesoft,dc=co,dc=uk
strRoot = "OU=Developers,dc=wisesoft,dc=co,dc=uk"

' Group to add
strGroupName = "Developers"

' *************************************************

SET objSystemInfo = CREATEOBJECT("ADSystemInfo") 
'Required for name translate
strNETBIOSDomain = objSystemInfo.DomainShortName

' Convert group name to distinguished name
strGroupDN =  GetDN(strNETBIOSDomain,strGroupName)

set objGroup = getobject("LDAP://" & strGroupDN)
set cmd = createobject("ADODB.Command")
set cn = createobject("ADODB.Connection")
set rs = createobject("ADODB.Recordset")

cn.open "Provider=ADsDSOObject;"
cmd.activeconnection = cn

cmd.commandtext = "<LDAP://" & strRoot & ">;" & strFilter & ";ADsPath,sAMAccountName;" & strScope

'**** Bypass 1000 record limitation ****
cmd.properties("page size")=1000

set rs = cmd.execute

' for each item returned by the Active Directory query
while rs.eof <> true and rs.bof <> true

	on error resume next
	' Add user to group
	objGroup.Add rs("ADsPath")

	' Error handling (user might already be a member of the group)
	if err.number = -2147019886 then
		wscript.echo "User '" & rs("sAMAccountName") & _
			"' is already a member of the '" & objGroup.sAMAccountName & "' group"
	elseif err.number <> 0 then
		wscript.echo "Error: " & err.number & " adding user '" & _
			 rs("sAMAccountName") & "' to the '" & objGroup.sAMAccountName & "' group"
	else
		wscript.echo "Added user '" & rs("sAMAccountName") & "' to the " & _
			objGroup.sAMAccountName & "' group"
	end if
	err.clear

	on error goto 0

	rs.movenext
wend

' Close ADO connection
cn.close

' Function to convert name into distinguished name format
Function GetDN(byval strDomain,strObject)
	' Use name translate to return the distinguished name
	' of a user from the NT UserName (sAMAccountName)
	' and the NETBIOS domain name.
	DIM objTrans

	Set objTrans = CreateObject("NameTranslate")
	objTrans.Init 1, strDomain
	objTrans.Set 3, strDomain & "\" & strObject
	GetDN = objTrans.Get(1) 

end function

 


Got a useful script? Click here to upload!


 

  Post Comment
Order By:  
User Comments
      

Posted On: 9/4/2018 12:59:05 AM

hogan gucci shoes softball bats new balance calvin klein outlet boy london clothing hermes bags nike air max 2017 kevin durant jersey wedding dress oakley sunglasses omega watches jordan 14 michael jordan tommy hilfiger converse all stars abercrombie clarisonic mia kenzo clothing basketball jersey montre homme nike blazer polo lacoste pas cher nike air zoom pegasus adidas outlet armani handbags nike roshe nike outlet cartier sunglasses ralph lauren outlet ferragamo belt dior sunglasses nike pas cher mont blanc pens rolex watches air max 2018 chrome hearts jewelry nike free cheap wedding dresses jordan 10 vans scarpe prada sunglasses burberry outlet cheap oakley sunglasses new balance femme fossil handbags nike vapor max oakley sunglasses celine handbags michael kors outlet online dak prescott jersey nike air oakley glasses new balance trainer replica watches air max babyliss pro nano titanium christian louboutin shoes karen millen adidas outlet store basketball shoes herve leger dresses marc jacobs handbags armani sunglasses ferragamo sunglasses converse uk england world cup jersey jordan 3 new balance shoes hollister outlet nike free run spain world cup jersey christian louboutin gentle monster sunglasses dolce & gabbana saint laurent handbags longchamp pas cher vans pas cher polo ralph lauren adidas zx flux mont blanc pens bcbg max azria g-star jeans arcteryx jackets coach purse coach outlet miu miu shoes mizuno running shoes adidas outlet store jordan 8 tory burch nike outlet online oakley pas cher nike shoes polo ralph lauren instyler pandora uk bose headphones true religion jeans chopard jewelry adidas crazy giuseppe zanotti outlet timberland boots mizuno true religion isabel marant shoes kate spade uk kobe bryant shoes nike air max 97 chloe handbags hogan shoes argentina world cup jersey lululemon outlet bally shoes insanity workout adidas running shoes ralph lauren lunette ray ban ralph lauren outlet new balance shoes polo ralph lauren kevin durant shoes swarovski jewelry supra shoes jordan xx9 givenchy jewelry 10 deep clothing kobe 11 air force 1 dansko outlet hermes nike shoes jordan 3 iphone x case jordan 4 ray ban sunglasses jordan 2 pandora bracciali chanel outlet beats headphones givenchy handbags nike air max antonio brown jersey timberland pas cher kobe 9 elite jordan 12 furla handbags nike clothing softball bats karen millen fitflops oakley vault lacoste jeans skechers sneakers beats by dre adidas shoes montre pas cher jimmy choo sunglasses oakley uk ghd hair straighteners longchamp outlet germany world cup jersey new balance soldes tommy hilfiger pandora jewelry kd 9 ray ban nike uk michael kors true religion christian louboutin lebron shoes jordan nike air max 90 vans outlet puma shoes france world cup jersey michael kors uk cheap jerseys michael kors outlet polo ralph lauren tory burch outlet nike air max mizuno nike air max jordan 1 bathing ape clarks shoes barbour nike mercurial nike outlet jordan 32 ray ban pas cher basket nike polo ralph lauren air force one mac cosmetics online adidas outlet store cartier bracelet mac cosmetics kates pade outlet online roshe run hermes birkin jordan pandora charms kyrie 4 tods shoes chloe handbags tn pas cher nike outlet nike air max alife clothing kids jordans jordan 7 soccer jerseys parajumpers fitflops insanity workout christian louboutin pas cher cheap oakley sunglasses guess handbags nike air max jordan pas cher jordan 6 sac longchamp salomon shoes giannis antetokounmpo jersey nike roshe run christian louboutin mulberry longchamp handbags nike free swarovski gioielli timberland boots brazil world cup jersey air max 95 converse jordan replica watches ralph lauren bulgari jewelry nike air force 1 aaron rodgers jersey rolex watches kevin durant shoes james harden jersey huf clothing nike huarache nike canada ray ban sunglasses gucci outlet jordan 6 coach outlet online air max guess air jordan true religion outlet nike blazer prada pandora jewelry goyard handbags nike air max 2017 curry 4 guess factory champion clothing burberry sunglasses cheap jerseys harry winston jewelry chloe sunglasses lebron 11 designer sunglasses michael kors uk abercrombie bcbg dresses christian louboutin yeezy shoes reebok outlet jordan 9 nike air max 95 prada outlet dyson hair dryer tory burch nfl jerseys dolce & gabbana orologi rolex converse outlet store supra shoes insanity adidas superstar bottega veneta sunglasses nike roshe gucci handbags converse shoes fendi sunglasses balenciaga sneakers true religion outlet burberry sale lululemon outlet nike air max ray ban sunglasses burberry beats headphones nike air max hermes bags nike blazer nike mercurial nike roshe run adidas trainers uk bottega veneta iphone cases lebron shoes cheap jordans toms outlet abercrombie adidas slides keen sandals nike outlet store guess canada burberry abercrombie and fitch carrera sunglasses michael kors outlet sac michael kors portugal world cup jersey fitflops outlet oakley sunglasses ai max ray ban sunglasses michael jordan nike mercurial superfly adidas soccer air max 90 occhiali oakley jordan 4 ferragamo lacoste shoes replica watches red bottom shoes prada handbags adidas soccer cleats chrome hearts jewelry michael kors outlet tiffany jewelry converse chuck taylor nfl jerseys asics gel adidas outlet store nike roshe jordan 5 chaussure nike longchamp bottega veneta coach factory outlet dsquared2 jeans salvatore ferragamo chi flat iron jordan shoes toms shoes ed hardy ferragamo marc jacobs handbags dolce & gabbana sunglasses michael kors canada gucci outlet sac vanessa bruno jordan lunette oakley kristaps porzingis jersey reebok shoes ghd flat iron barbour jackets cheap jordan shoes scarpe hogan chanel handbags belgium world cup jerseys manolo blahnik shoes swarovski crystal nike foamposite alexander mcqueen handbags air max 90 jimmy choo furla handbags hollister air more uptempo kate spade dansko shoes cheap wedding dresses nike free ray ban sunglasses dsquared2 outlet kyrie irving jersey michael kors handbags freshjive clothing golden goose sneakers nike roshe run air max plus ray ban occhiali prom dresses kd 10 longchamp handbags vibram fivefingers ferragamo shoes louboutin pas cher michael kors handbags carson wentz jersey dolce & gabbana adidas originals tommy hilfiger pas cher nike free run diesel jeans burberry outlet versace handbags converse curry 3 ray ban jordan clothing hermes jewelry bvlgari sunglasses armani jeans nike free run reebok shoes flip flops asics gel vans outlet chanel outlet true religion jeans curry 5 nike factory ferragamo shoes dior bags nike air max 90 valentino fendi handbags calvin klein jeans asics running shoes gucci belt nike store kobe 12 jordan 31 ai max adidas flip-flops beats pill links of london uk burberry clothing pandora bijoux oakley sunglasses marc jacobs handbags air max 90 adidas superstar stuart weitzman shoes nike kyrie 2 michael kors asics trainers jordan 13 mac makeup air jordan nike shoes curry 2 jbl headphones ghd fendi basket store louboutin outlet stuart weitzman jimmy choo shoes jordan 11 sac michael kors burberry handbags kobe 10 balmain jeans montres pandora charms bape clothing jordans mac makeup michael jordan adidas clothing longchamp outlet bijoux swarovski michael kors outlet michael kors 2018.9.4xukaimin