[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Moving Computer Objects to a new OU VBScript stuck in loop

Posted on 2009-02-18
11
Medium Priority
?
1,214 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

0
Comment
Question by:tneubauertocg
  • 6
  • 4
11 Comments
 
LVL 12

Expert Comment

by:Krys_K
ID: 23672034
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
0
 

Author Comment

by:tneubauertocg
ID: 23672174
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...
0
 
LVL 12

Expert Comment

by:Krys_K
ID: 23672329
HI

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

What errors occur?

Cheers
Krystian
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:tneubauertocg
ID: 23672340
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.
0
 
LVL 12

Expert Comment

by:Krys_K
ID: 23672431
HI

you have mispelt
ADsDSOObeject

should be

ADsDSOObject

Krystian
0
 

Author Comment

by:tneubauertocg
ID: 23672493
Ooops - Now I am getting "The search filter cannot be recognized" on line 68.

Thanks for your persistance.
Ted...
0
 
LVL 12

Expert Comment

by:Krys_K
ID: 23672831
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
0
 
LVL 12

Accepted Solution

by:
Krys_K earned 1000 total points
ID: 23672906
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
0
 

Author Comment

by:tneubauertocg
ID: 23673190
That did it - THANK YOU very much Krystian.

0
 
LVL 12

Expert Comment

by:Krys_K
ID: 23673210
Your welcome. Gald it's working for you now
Thanks for the grade and points


With Regards
Krystian
0
 

Expert Comment

by:jmwagner
ID: 25704357
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
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question