Link to home
Start Free TrialLog in
Avatar of tneubauertocg
tneubauertocgFlag for United States of America

asked on

Moving Computer Objects to a new OU VBScript stuck in loop

I am trying to create a script that moves a group of computer objects (based on a .txt file) from their existing OU to a new OU.  The script works as expected except for the fact that it gets stuck in the "Do Until objRecordSet.EOF" loop at the end of the script.

The script allows you to browse to a .txt file that contains the list of computer objects.  This list is brought into the "arrComputer" object.  The array does get filled but it appears that only the first computer object in the array is processed over and over again in an endless loop.

As a secondary item on my wish list would be to have the script output the list of computer objects and their original OU to a .csv file.  This would assist me in moving them back to their original location after they have completed the testing.

Your assistance is GREATLY appreciated.
Option Explicit
 
Dim arrComputers, objComputer
 
arrComputers = GetComputerList()
   
	For Each objComputer In arrComputers
	   Call MoveComputerObject	
        Next
 
Function GetComputerList()
Dim oFSO, fso, oDialog, intResult, oTxtFile, aComputers
On Error Resume Next      
   
      fso = "Scripting.FileSystemObject"
      Set oFSO = CreateObject(fso)
      Set oDialog = CreateObject("UserAccounts.CommonDialog")
	   
      'configure the dialog box
      oDialog.Filter = "Text Files|*.txt|All Files|*.*"
      oDialog.FilterIndex = 1
      oDialog.InitialDir = ""
       
      'trap error and close
      intResult = oDialog.ShowOpen
      Select Case intResult
         Case True	If Err <> 0 Then Quit(1)
            If Len(Trim(intResult)) = 0 Then Quit(2)
         Case False Quit(1)
      End Select
	   
      'open the file
      Const ForReading = 1   
      Set oTxtFile = oFSO.OpenTextFile(oDialog.FileName, ForReading)
 
      'read the file
      aComputers = oTxtFile.ReadAll
      aComputers = Split(aComputers, vbCrLf)
      oTxtFile.Close
   
      GetComputerList = aComputers
      
On Error GoTo 0
   
End Function
 
 
Sub MoveComputerObject
On Error Resume Next
 
Const ADS_SCOPE_SUBTREE = 2
 
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObeject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
 
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
 
objCommand.CommandText = _
    "SELECT ADsPath FROM 'LDAP://DC=corp,DC=company,DC=org' WHERE objectCategory='computer' " & _
        "AND name=" & "'" & objComputer & "'"
Set objRecordSet = objCommand.Execute
 
 
objRecordSet.MoveFirst
	Do Until objRecordSet.EOF
	    strADsPath = objRecordSet.Fields("ADsPath").Value
	    Set objOU = GetObject("LDAP://OU=Desktops,OU=Clients,OU=GPO Test,DC=corp,DC=company,DC=org")
	    intReturn = objOU.MoveHere(strADsPath, vbNullString)
	    objRecordSet.MoveNext
	Loop
End Sub

Open in new window

Avatar of Krys_K
Krys_K
Flag of United Kingdom of Great Britain and Northern Ireland image

HI There

On line 72 you cannot do that.

you need to change it to

objOU.MoveHere strADsPath, vbNullString

instead of

intReturn = objOU.MoveHere(strADsPath, vbNullString)


Regards
Krystian
Avatar of tneubauertocg

ASKER

Krystian,

I made the change you suggested and the behavior has not changed, it still gets stuck in the loop.

Any other ideas?

Thanks,
Ted...
HI

If you turn on error resume next off
comment if out with '

What errors occur?

Cheers
Krystian
I just commented out my "on error resume next" line and found that the error "Provider cannot be found.  It may not be properly installed." comes up on line 56.
HI

you have mispelt
ADsDSOObeject

should be

ADsDSOObject

Krystian
Ooops - Now I am getting "The search filter cannot be recognized" on line 68.

Thanks for your persistance.
Ted...
Hi
comment out line 68 as its not really needed

Might help to out some
Wscript.Echo
statements in a few places to help work out where yuor script is getting to before a failure occurs

So after
      Do Until objRecordSet.EOF
          strADsPath = objRecordSet.Fields("ADsPath").Value

Put
Wscript.Echo strADsPath

Then you will see which computers it works on and which is the last one it fails on if atall

Also, its better to run in a command window as
CScript.exe Script.vbs

Then the popups will output to the window instead of as a message box that you need to click OK on each time


Krystian
ASKER CERTIFIED SOLUTION
Avatar of Krys_K
Krys_K
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That did it - THANK YOU very much Krystian.

Your welcome. Gald it's working for you now
Thanks for the grade and points


With Regards
Krystian
Avatar of jmwagner
jmwagner

Krystian,

I'm new to scripting and am trying to utilize this script and I'm not having any luck.

Will this script move a machine from ANY particular OUs they are in, or does the original container need to specified?
For example:
("SELECT ADsPath FROM 'LDAP://DC=)

I have computers in multiple OUs and wanted to know if this would work to move them to a common OU.
Thank you