Subscribe to Windows IT Pro
November 03, 2003 12:00 AM

Rem: Moving Users Between OUs

Windows IT Pro
InstantDoc ID #40521
Rating: (1)
Downloads
40521.zip

I'm creating a script that moves users from one Active Directory (AD) organizational unit (OU) to another AD OU according to data in a comma-separated value (CSV) input file. As Figure 1 shows, each line of the input file contains two fields: the user's common name (cn) and computer type (Desktop or Laptop). I'm using the computer type to control the OU that each user moves to. For example, I'm moving users with desktop computers from the ou=corp,dc=MyDomain,dc=com OU to the ou=Desktops,dc=MyDomain,dc=com OU and users with laptop computers from the ou=corp,dc=MyDomain,dc=com OU to the ou=Laptops,dc=MyDomain,dc=com OU. I've had success opening the input file and creating a log file. My dilemma is parsing the data and performing the move operation. Can you point me in the right direction?

You bet—Listing 1, page 10, is a reworked version of the UserMove.vbs script you sent me. Let's walk through the script, which should help you identify where you took a wrong turn.

The first 17 lines are almost identical to those of your original script. These lines contain the script's directives (i.e., Option Explicit and On Error Resume Next), variable declarations, and constants. Of particular importance are the INPUTFILE and LOGFILE constants and the three OU-related constants: DESKTOP_OU_DN, LAPTOP_OU_DN, and LEGACY_OU_DN. You'll need to modify the values assigned to these constants according to your network's specific configuration.

At callout A, the script creates a reference (objFSO) to the Scripting Runtime Library's FileSystemObject object. The script uses the objFSO reference variable to open the input and log files. In the event the script can't create a reference to the FileSystemObject object, the script uses the VBScript MsgBox function to display an appropriate error message and exits.

The code at callout B opens the input file for reading by using the objFSO variable in combination with the FileSystemObject object's OpenTextFile method. If the input file fails to open, the script displays an appropriate error message and exits. Provided that the input file opens, the script uses the objInputFile variable (also known as a file handle or text stream) in conjunction with the TextStream object's ReadAll method to read the entire input file into memory. The script passes the in-memory file to the VBScript Split function, which splits the input file into substrings at each carriage return/linefeed. The result is an array in which each element contains one line of the input file, as Figure 2, page 10, illustrates. After initializing the arrUserRecords array, the script closes the input file.

The code at callout C creates the log file or opens the log file if it already exists. If the script can't create or open the log file, the script displays an error message and exits. Provided that the script can open the log file, the script uses the objLogfile variable (also a file handle or text stream) to write the script's start time to the log file, then closes the log file. The purpose of closing the log file after each write operation is to ensure that the log file captures the last action that the script performed. If you don't close the log file after important write operations and an error causes the script to end unexpectedly, the log-file data will be lost. Determining where an error occurred is difficult if you don't have accurate log-file data.

Callout D highlights where the script's real work begins. Here the script uses Active Directory Service Interfaces (ADSI) to connect to the two target OUs: ou=Desktops and ou=Laptops. Your script took a wrong turn at this point by connecting to the source OU.

Related Content:

ARTICLE TOOLS

Comments
  • ROB
    8 years ago
    Sep 01, 2004

    I am doing something similiar with an excel spread sheet using the movehere method but my script always fails on line 11 when i try and insert the argument from my spread sheet. My goal is to use data parsed from active directory with Hyena to rename user objects within an OU I have sucessfully used this script to modify mutiple attributes of about 650 users in our domain. It seems that the objou.move here method doesn't allow me to insert an argument.

    Set objExcel = CreateObject("Excel.Application")
    Set objWorkbook = objExcel.Workbooks.Open("C:\\Scripts\\moveuser.xls")
    intRow = 2

    Do Until objExcel.Cells(intRow,1).Value = ""


    Set objUser = GetObject("LDAP://us0-dc01/" & objExcel.Cells(intRow, 1).Value)
    objOU.MoveHere("LDAP://us0-dc01/", & objExcel.Cells(intRow, 2).Value) , ("cn=", & objExcel.Cells(intRow, 3).Value)












    intRow = intRow + 1


    Loop

    objExcel.Quit

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.