By David Wiseman (Administrator)Created 03 Jul 2008
My Rating:
Vote
Rating:
Not Rated
Views:11875
Downloads:144

Move All Users In Department to a Specified OU

Language:  VBScript

Compatibility

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

This script will move all the users in the "Marketing" department to the "Marketing" OU. Simply modify the setup section to suite your own requirements.

Code

Line Numbers: On  Off      Plain Text
OPTION EXPLICIT
Const ADS_PROPERTY_CLEAR = 1

DIM strSearchFilter, strSearchRoot, objRootDSE
DIM cn,cmd,rs, strSearchScope
DIM objNewOU, strNewOU

' ********************************************************
' * Setup
' ********************************************************

' Specify the distinguished name of the new OU
strNewOU = "ou=Marketing,ou=All Users,dc=wisesoft,dc=org,dc=uk"

' Modify the filter to query for your department.  
' This filter will find all users in the marketing department
strSearchFilter = "(&(objectCategory=Person)(objectClass=User)(department=marketing))"

' Specify a search root. The domain root is used by default. 
' e.g. dc=wisesoft,dc=co,dc=uk
' You could also specify a particular OU to start the search from.
' e.g. strSearchRoot = "ou=students,ou=All Users,dc=wisesoft,dc=co,dc=uk"
strSearchRoot = getDomainRoot

' A value of "subtree" will search all child containers (OUs).
' Change to "onelevel" if you don't want child containers to be 
' included in the search
strSearchScope = "subtree"

' ********************************************************
set objNewOU = GetObject("LDAP://" & strNewOU)

Set cn = CreateObject("ADODB.Connection")
Set cmd =   CreateObject("ADODB.Command")
cn.open "Provider=ADsDSOObject;"

Set cmd.ActiveConnection = cn

cmd.CommandText = "<LDAP://" & strSearchRoot & ">;" & strSearchFilter & ";ADsPath;" & strSearchScope
cmd.Properties("Page Size") = 1000

Set rs = cmd.Execute

' loop through the search results
while rs.eof<> true and rs.bof<>true
	' Move user to new ou (passing the ADsPath attribute returned from the query)
	objNewOU.MoveHere rs(0),vbNullString

	rs.movenext
wend

rs.close
cn.close

wscript.echo "Completed"

private function getDomainRoot
	' Bind to RootDSE - this object is used to 
	' get the default configuration naming context
	' e.g. dc=wisesoft,dc=co,dc=uk

	set objRootDSE = getobject("LDAP://RootDSE")
	getDomainRoot = objRootDSE.Get("DefaultNamingContext")
end function

 


Got a useful script? Click here to upload!


 

  Post Comment
Order By:  
User Comments
      
happykapoor678
Posted On: 10/11/2017 8:34:30 PM


Karan
Posted On: 1/20/2018 2:01:08 AM

I am very happy to join this community Phase 10 Rules

Eamcetresult
Posted On: 3/1/2018 11:04:47 PM

The Telangana State Government holds Telangana Eamcet Entrance every year to all the applied applicants; the Jawaharlal Nehru Technological University (JNTUH) conducts the TS Eacmet Exam every year on behalf of Telangana State Council Of Higher Education. TS Eamcet Results 2018


Posted On: 3/23/2018 10:51:47 PM

It is a good and amazing article. Your information is very useful for me and for others. thank you so much.Great post. I like this post. walmartone | walmartone login | walmartone.com

wordsdoctorate
Posted On: 4/5/2018 2:08:20 AM


tnresults
Posted On: 4/6/2018 11:04:24 AM

The Tamil Nadu state board of secondary education is responsible for conducting the SSLC and HSC exams in the state of Tamil Nadu.The procedure for the downloading of the result of SSLC and HSC examinations are shown in the article. dge2.tn.nic.in www.dge2.tn.nic.in

universesthetique
Posted On: 4/10/2018 3:52:46 AM


mikeshean11
Posted On: 5/6/2018 11:45:46 AM

Watch online movies and tv shows with terrarium tv. terrarium tv allow you to stream movies and tv shows online. You can also enjoy movies on terrarium tv pc and other android powered device.

mikeshean11
Posted On: 5/6/2018 11:45:46 AM

Watch online movies and tv shows with terrarium tv. terrarium tv allow you to stream movies and tv shows online. You can also enjoy movies on terrarium tv pc and other android powered device.

mikeshean11
Posted On: 5/6/2018 11:45:46 AM

Watch online movies and tv shows with terrarium tv. terrarium tv allow you to stream movies and tv shows online. You can also enjoy movies on terrarium tv pc and other android powered device.

anandaditi
Posted On: 5/15/2018 4:39:29 AM

Are you a health freak? Do you want to get a toned body and build up a strong physique by working out regularly?When it comes to building your body muscles or gain some more weight, in case you are too lean, nothing can beat whey protein. check here best protein supplement in India


Posted On: 5/15/2018 4:44:22 AM

Vitamix is one of the most well-known brands or blenders, that offers excellence in terms of performance and also when it comes to managing time. Vitamix 5300 Review

allreview4u
Posted On: 5/19/2018 6:05:02 AM


allreview4u
Posted On: 5/19/2018 6:05:04 AM


allreview4u
Posted On: 5/19/2018 6:05:07 AM


123