We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Moving Computer Objects to a new OU VBScript stuck in loop

tneubauertocg
on
Medium Priority
1,227 Views
Last Modified: 2012-05-06
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

Comment
Watch Question

Commented:
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

Author

Commented:
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...

Commented:
HI

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

What errors occur?

Cheers
Krystian

Author

Commented:
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.

Commented:
HI

you have mispelt
ADsDSOObeject

should be

ADsDSOObject

Krystian

Author

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

Thanks for your persistance.
Ted...

Commented:
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
Commented:
Also

That error was to do with a blank line in your file (the last line)
To cater for this put

For Each objComputer In arrComputers
 If objComputer <> "" then
    Call MoveComputerObject      
 End If
Next

Then you can leave Line 68 in if you want

Krystian

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
That did it - THANK YOU very much Krystian.

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


With Regards
Krystian

Commented:
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
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.