Winscripter
  WSH
  Forums
  Downloads
  Books
  Links
  Amazon




Login
Register

© winscripter.com
1998-2004







Script: Move Active Directory Objects

Posted by on Wednesday, May 12, 2004 (PST)

Script moves a user and a computer object from one OU to another. Using LDAP path it binds to destination OU and Executes the MoveHere method of ADSI Container object.

 

/**
 *    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 );
}

Comments:

Moving computer to another OU
By mack on Tuesday, July 05, 2005 (PST)
Could this be edited to pick up the computer name it is being run on and move or copy it to another OU instead of already having the computer name inserted already



WSH and ADSI Administrative Scripting

New Articles
  • List installed COM objects and associated ProgIDs
    Script: Lists all COM Objects and their associated ProgIDs (If available). Win32_ClassicCOMClassSetting

  • Script: File Rotator
    Script: Rotate files where the most current file has the lowest number in the archive. When files exceed the retention period, they are deleted. Typically used for log files, backups, etc..

  • Script: Create IIS Website and DNS record
    Script: Dan Casier sent me this script that will create a website and appropriate DNS record. The script is intended for Windows 2000 Server with local DNS and necessary DNS mof installed.


  • Winscripter   |  WSH   |  Forums   |  Downloads   |  Books   |  Links   |  Amazon