d

Move Active Directory Object

How to move an AD object from one OU to another.

/**
 *    Script:   moveADObject.js
 *    Purpose:  Moves Active Directory Objects from
 *              one OU to another.
 *    Author:   Daren Thiel 
 *    Date:     12 May 2004 
 *    Web:      http://www.winscripter.com
 *   
 *    Note:     Hardcoded example that moves the user named
 *              George Jetson from the Sales\East OU to the
 *              Sales\West OU.  We also move George's computer
 *              account as well.
 **/

// Define User  
var userobj = "CN=George Jetson";
var compobj = "CN=Comp1";

// Path to OU which contains user account
var srccont = "OU=East,OU=Sales,DC=pdc,DC=tse,DC=com";

// Path to destination OU
var dstcont = "OU=West,OU=Sales,DC=pdc,DC=tse,DC=com";

// Call our function
MoveObject( userobj, srccont, dstcont );
MoveObject( compobj, srccont, dstcont );

// Move Object from one OU to another
function MoveObject( userobj, srccont, dstcont )
{
    try
    {
        // Build LDAP Paths
        var ldapdst = "LDAP://" + dstcont;
        var ldapusr = "LDAP://" + userobj + "," + srccont;
        
        // Bind to Destination Container
        var dcon = GetObject( ldapdst );
        
        // Move object to destination OU
        dcon.MoveHere( ldapusr, userobj );
    }
    catch( e )
    {
        print( "Error: " + e.description );
    
        // Check for 'object not found'  normally a path
        // problem.  Additional error codes can be trapped
        // for, see MDSN 'system error codes' for more.
        if( ( e.number & 0xFFFF ) == 8240 )
            print( "Object not found" );
        else
            print( "  Num: " + ( e.number & 0xFFFF ) );
    }
}

function print( msg )
{
    WScript.Echo( msg );
}