• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2115
  • Last Modified:

VB Script to move computers in AD

I need a script that will move a computer from the "Computers" container in Active directory to an OU called "Test" inside of an OU called "Workstations"

Here's a diagram of how it looks
My.Domain.net
   Computers
         MyComputer
   Workstations
         Test
             (This is where I'd like MyComputer to move to)

I hope that made sense.  This is the script I'm currently using and running into errors with.

dim objOU, intReturn, objRootDSE, strDNSDomain
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("DefaultNamingContext")
Set objOU = GetObject("LDAP://OU=Workstations,OU=Test," & strDNSDomain)
intReturn = objOU.MoveHere _
    ("LDAP://CN=MyComputer,CN=Computers," & strDNSDomain, vbNullString)

I'm currently getting this Windows Script Host Error Message
Script: C:/blah/blah/blah/move.vbs
Line: 5
Char: 1
Error: There is no such object on the server.
Code: 8072030
Source: (null)



0
whiting002
Asked:
whiting002
  • 3
  • 2
2 Solutions
 
gecko_au2003Commented:
According to that error and what code you have shown me :

It is having an error with thie :

intReturn = objOU.MoveHere _
    ("LDAP://CN=MyComputer,CN=Computers," & strDNSDomain, vbNullString)

So does that mean MoveHere does not exist and you would need to find the actual command to move it ? Maybe try :

intReturn = objOU.Move _
    ("LDAP://CN=MyComputer,CN=Computers," & strDNSDomain, vbNullString)

Here are some sites :

http://www.computerperformance.co.uk/ezine/ezine27.htm

http://www.codecomments.com/archive300-2004-4-183910.html

Does any of that help you out :)
0
 
gecko_au2003Commented:
I looked at those sites the your command in terms of the MoveHere part was correct, those sites will be able to help you out more though :)
0
 
JesterTooCommented:
I don't believe you can move the contents of the CN this way... you need to iterate through the collection of computers and move each one individually.  You also need to name the OU's in "inside out" order... grandchildren, children, parent.  Here is a script that works for me in my W2K domain.

dim objOU, intReturn, objRootDSE, strDNSDomain

Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("DefaultNamingContext")

Set oTo   = GetObject("LDAP://OU=Test,OU=Workstations," & strDNSDomain)
Set oFrom = GetObject("LDAP://CN=MyComputer,CN=Computers," & strDNSDomain)
oFrom.Filter = Array("computer")

For Each oComputer In oFrom
   oTo.MoveHere oComputer.AdsPath, vbNullString
Next  

HTH,
Lynn
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
JesterTooCommented:
oooh,,, I just reread your question... I'd originally thought you were moving all the computers in the container"MyComputer"... since you're just moving a specific computer (named MyComputer), you only need to change 1 line of your script... Here is the correct line:

    Set objOU = GetObject("LDAP://OU=Test,OU=Workstations," & strDNSDomain)

Just reverse the order of the OU's so the child is named before its parent.  Also, the MoveHere method does not have a return value so you don't need the part "intReturn =" nor the parentheses around the method's parameters.

-- Lynn

0
 
whiting002Author Commented:
Hey sorry guys I just saw that this was still open.  I got this working a WHILE ago and I beleive I used both of your comments for help so I'm dividing the points.  Sorry I took so long.
0
 
gecko_au2003Commented:
Thanks whiting for the grade and points :) I hope you got it sorted out :)
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now