Code Script .co.uk

Home | Scripts | Tutorials | Disclaimer | Sitemap | Contact

   Move Users And Groups Between Ous
 

Vbscript / Active Directory / Move Users And Groups Between Ous


'Access account details
Const Username = "sdcwdc01administrator"
Const Password = "password"

'Target domain
Const NTDomain     = "WinNT://ntdomain"
Const DC1 = "domain"
Const DC2 = "local"

set oLDAP = GetObject("LDAP:")

set oFSO =Wscript.CreateObject ("Scripting.FileSystemObject")
Set oLogFile  = oFSO.OpenTextFile(oFSO.GetAbsolutePathName(".") & "InterOUMoves.log", 8, true)

oLogFile.WriteLine vbCRLF & vbCRLF & "Script started: " & Now()


'Function call examples
MoveUsers "Domain Admins", "Users", "Admins"

MoveGroups "Users", "ou=Groups,OU=Admins"

WScript.Echo "Done"

oLogFile.Close


'Move users in group TargetGroup in OU SourceOU to OU DestOU
Sub MoveUsers(TargetGroup, SourceOU, DestOU)

Dim GroupName, CurrentUserOU, DestinationOU


oLogFile.WriteLine vbCRLF & "Moving users in " & TargetGroup & " from " & SourceOU & " to " & DestOU


' Group name containing users to be moved:
GroupName= TargetGroup

' OU users accounts reside:
CurrentUserOU   = "CN=" & SourceOU & ",DC=" & DC1 & ",DC=" & DC2

' OU users will be moved to:
DestinationOU   = "LDAP://" & DC1 & "." & DC2 & "/OU=" & DestOU & ",DC=" & DC1 & ",DC=" & DC2


Set oDomain  = GetObject(NTDomain)
Set oGroup   = oDomain.GetObject("Group", GroupName)
Set oMembers = oGroup.members

set oDestinationOU = oLDAP.OpenDSObject(DestinationOU,Username,Password,1)

sUserToMove= ""
For Each oPerson In oMembers
 sUserToMove = oPerson.name

 On Error Resume Next

 oDestinationOU.MoveHere "LDAP://CN=" & sUserToMove & "," & CurrentUserOU, "CN=" & sUserToMove
 if err.number <> 0 then
  oLogFile.WriteLine "User not moved: " & sUserToMove & ", from " & SourceOU
 else
  oLogFile.WriteLine "User moved: " & sUserToMove & ", from " & SourceOU & " to " & DestOU
 end if 
 On Error Goto 0
Next

End Sub

'Move groups residing in SourceOU to DestOU
Sub MoveGroups(SourceOU, DestOU)

Dim CurrentUserOU, DestinationOU

oLogFile.WriteLine vbCRLF & "Moving groups in " & SourceOU & " to " & DestOU

' OU users accounts reside:
CurrentUserOU   = "CN=" & SourceOU & ",DC=" & DC1 & ",DC=" & DC2

' OU users will be moved to:
DestinationOU   = "LDAP://" & DC1 & "." & DC2 & "/" & DestOU & ",DC=" & DC1 & ",DC=" & DC2

dim oDomain, oGroup, oMembers, oPerson, oDestinationOU
dim sGroupName, sUserToMove

Set oDomain  = GetObject(NTDomain)

odomain.filter = Array("group")


set oDestinationOU = oLDAP.OpenDSObject(DestinationOU,Username,Password,1)
 

sUserToMove= ""
For Each oGroup In oDomain
 sUserToMove = oGroup.name

 On Error Resume Next

 oDestinationOU.MoveHere "LDAP://CN=" & sUserToMove & "," & CurrentUserOU, "CN=" & sUserToMove
 if err.number <> 0 then
  oLogFile.WriteLine "Group not moved: " & sUserToMove & ", from " & SourceOU
 else
  oLogFile.WriteLine "Group moved: " & sUserToMove & ", from " & SourceOU & " to " & DestOU
 end if 
 On Error Goto 0
Next


End Sub





Please note that a disclaimer applies to any code on this page.
 
   Actions
  Go Back
  Bookmark
  Print Page


   Menu
 
- Links
- Reference
- Script Editors
- Tutorials
- Vbscript
     - Active Directory
          - Ad User Information Query
          - Add Machine To Domain
          - Create Ou Structure
          - Enumerate Ad Users
          - Enumerate Group Members
          - Move Users And Groups Between Ous
          - Raise Domain Functional Level
     - Exchange
     - Files And Folders
     - General
     - Ms Office
     - Operating System
     - Processes And Services
     - Text Processing
     - User Interaction
     - Web Servers