Re: Moving objects between OUs

Giganews Newsgroups
Subject: Re: Moving objects between OUs
Posted by:  Erik Cheizoo (echeizoo.XenD.nl@removethis.gmail.com)
Date: Wed, 3 Jan 2007

Robert,

The ADS function you'll need to use is called MoveHere.
Below is a sample VBS script you can use as a starting point.
The script will change the CN of all users in an OU using the MoveHere
function, but can be easily modified to move objects from one OU to another.
Just make sure the strNewName contains the full path where the object must
be moved to (including the object name itself which is available in strCN).
For completeness: first try this on test objects to make sure the script
does exactly what you want :p

Kind regards,

Erik Cheizoo
eXcellence & Difference
We keep your business running

'********************************************************************
' VB Script Document
' Erik Cheizoo
' eXcellence & Difference

option explicit

Dim oArgs
Dim objAdsPath
Dim strAdsPath
Dim strMessage
Dim User
Dim return
Dim strSamAccountName, strCN, strOldName, strNewName

'Rename CN using the MoveHere function

Set oArgs = WScript.Arguments
If oArgs.Count <> 1 then
  strMessage = "Error in command line!" & vbCRLF & _
              "Usage: cscript RenameCN ""<LDAP target container>""" &
vbCRLF & vbCRLF & _
              "Do not forget to surround <LDAP target container> with
quotes if it contains spaces." & vbCRLF & _
              "e.g. ""OU=Users, OU=Test, DC=contoso, DC=com"""
  ErrorHandler strMessage
End If

'*** Bind to OU (passed on commandline)
  strAdsPath = oArgs(0)
  on error resume next
    Set objAdsPath = GetObject( "LDAP://" & strAdsPath )
    If Err <> 0 then ErrorHandler "LDAP path not found, check command line"
  on error goto 0

'**** Show connected AdsPath and ask for confirmation
strMessage = "Connected path: " & objAdsPath.distinguishedName & vbCRLF &
vbCRLF & _
            "Are you sure to rename all CN's to samAccountName located in
this container?"
return = MsgBox(strMessage, vbExclamation + vbOKCancel, "Please confirm
action")
If Not return = vbOK then ErrorHandler "Operation aborted, no changes made"

'*** Loop through all user objects
  objAdsPath.Filter = Array("user")
  For Each User in objAdsPath
    strSamAccountName = user.SamAccountName
    strCN = User.CN

    wscript.echo strSamAccountName

      'Set new name to sAMAccountName
      strOldName = user.distinguishedName
      strNewName = "CN=" & user.DisplayName  'used to rename to display
name

      wscript.echo vbTab & "Old name: "& strOldName
      wscript.echo vbTab & "New name: "& strNewName

      on error resume next
      'Rename CN by using the MoveHere method of the object
      objAdsPath.MoveHere "LDAP://" & strOldName, strNewName
      if Err <> 0 then ErrorHandler "(" & Err.number & ")  Error while
renaming (moving) object!"
      on error goto 0
      wscript.echo vbTab & ">>> Renamed/Moved"

  Next

'Done!
'********************************************************************

"supersonic_oasis" <supersonicoas…@discussions.microsoft.com> wrote in
message news:F8142632-DAFC-4F4F-96B7-742DC2F3AA…@microsoft.com...
> Hello,
>
> I know there is a way to move users between Organizational Units from the
> GUI, but I was wondering if there was a way to do it from the command
> line,
> so I can script it?  I want to be able to move them from One OU to another
> within the same domain.
>
> Robert

Replies

None

In response to

Moving objects between OUs posted by supersonic oasis on Mon, 1 Jan 2007