title: active directory copying user attributes
body: <p>I have a script, which should copy <code>Description</code> value to <code>Department</code>.
  But script only seems to be working 50%. </p>
  
  <p>I have <code>OU</code>, and there are 3  sub <code>OU</code>'s. 
  The script changes only first <code>OU</code> users attributes, but not the other 2.
  Could someone check script and tell me whats wrong with it.</p>
  
  <p>Script:</p>
  
  <pre><code>Option Explicit 
  
  On Error Resume Next 
  Dim objUser, objChild, objConnection, objRootDSE, objItem
  Dim WshShell, objFSO, strRoot, strDNSDomain, strContainer
  Dim strDescription, strsAMAccountName 
  Dim strdepartmentAfter, strDirectory, strdepartmentBefore
  Dim i, intLogFlag 'no log exists
  
  i=1 
  intLogFlag = 0
  
  Set WshShell = CreateObject("WScript.Shell") 
  
  'Set current directory to Desktop &amp; display on page 
  strDirectory = WshShell.SpecialFolders("Desktop") &amp; "\" 
  Set objRootDSE = GetObject("LDAP://RootDSE") 
  strDNSDomain = objRootDSE.Get("DefaultNamingContext") 
  strContainer = strContainer &amp; strDNSDomain 
  'To do a subcontainer, add it after//ou=OUName, - include comma 
  Set strRoot =GetObject("LDAP://OU=Kasutajad," &amp; strDNSDomain ) 
  'Start Logging 
  CreateLog() 
  '**************************************************************** 
  For each objChild in strRoot 
  Select Case objChild.class 
  Case "organizationalUnit","container" 
  Call DATree 
  End Select 
  Next 
  
  Sub DATree() 
  OU.Filter="objectClass=user"
  For each user in OU
  If user.class="user" Then
      On Error Resume Next
      user.Department=user.description
      If Err.Number = 0 Then
           user.SetInfo
           WScript.Echo "Copied:" &amp; user.description &amp; " for " &amp; user.name 
      Else
           On Error GoTo 0
           WScript.Echo "No IPPHONE configured for " &amp; user.name
      End If
      On Error GoTo 0
  end if
  i=i+1 
  next 
  End Sub 
  
  i = i -1 
  Wscript.Echo "Accounts = " &amp; i 
  Wscript.Quit 
  
  '**************************************************************** 
  
  Sub CreateLog() 
  On Error Resume Next 
  Dim objFile 
  Dim strFile, strText 
  
  'Create log file 
  strFile = "UserDepartmentLog_" &amp; Month(Date()) &amp; "_" &amp; Day(Date()) &amp; ".txt" 
  Set objFSO = CreateObject("Scripting.FileSystemObject") 
  Set objFile = objFSO.CreateTextFile(strDirectory &amp; strFile) 
  Set objFile = Nothing 
  'Write headers to the log file 
  strText = "User Name,Date,Description,Department Before, Department After" 
  Set objFile = objFSO.OpenTextFile(strDirectory &amp; strFile, 8, True) 
  objFile.WriteLine(strText) 
  intLogFlag = 1 
  Set objFSO = Nothing 
  Set objFile = Nothing 
  End Sub 
  
  '**************************************************************** 
  
  'Used to append the log for each computer the script is run against 
  
  Sub WriteLog(strdescription, strAccountName, strDeptBefore, strDeptAfter) 
  On Error Resume Next 
  Dim objFile, objTextFile 
  Dim strFile, strText 
  
  strFile = "UserDepartmentLog_" &amp; Month(Date()) &amp; "_" &amp; Day(Date()) &amp; ".txt" 
  Set objFSO = CreateObject("Scripting.FileSystemObject") 
  'Check to see if the log exists 
  If intLogFlag = 1 Then 
  'Write to the log 
  strText = strAccountName &amp; "," &amp; Date() &amp; "," &amp; strDescription &amp; "," &amp; strDeptBefore &amp;                         "," &amp; strDeptAfter 
  
  Set objFile = objFSO.OpenTextFile(strDirectory &amp; strFile, 8, True) 
  objFile.WriteLine(strText) 
  objFile.Close 
  'Reset strText for later use 
  strText = "" 
  Else 'If the log doesn't exist, create it 
  CreateLog() 
  'Reset strText for later use 
  strText = "" 
  End If 
  Set objTextFile = Nothing 
  Set objFile = Nothing 
  Set objFSO = Nothing 
  End Sub
  </code></pre>