Subscribe to Windows IT Pro

Forums

Welcome to the Windows IT Pro community forums! The forums are designed to give Microsoft Windows IT professionals and system administrators a gathering place to ask technical questions, get answers to problems, and provide solutions for forum posters. We have forum areas dedicated to popular topics like Microsoft Exchange and Outlook, Windows Server 8, virtualization, security, and Windows 8, as well as many more.

We'd encourage you to register so you can become an active forum participant by contributing your own questions and answers.

Our forums are moderated and maintained by our Forum Pros, who are all veteran IT professionals with extensive hands-on, real-world IT experience. Please visit our Meet the Forum Pros page to learn more about these IT experts. We also have community forums dedicated to Microsoft SharePoint and Microsoft SQL Server topics as well.

VBScript to move this computer to a different OU?

Last Post 14 Feb 2007 07:47 AM by nickraj12. 4 Replies.
AddThis - Bookmarking and Sharing Button Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
ralphsthomas
Rookie
Rookie
Posts:2

--
18 Aug 2006 07:39 AM
Hi! I'm looking to move a bunch of Windows 2000 boxes into a different OU. But I want to do so while logged in at their consoles, not remotely or in a batch. If these were Windows XP machines, I'd simply run the following commands:

dsmove -newparent "ou=new,dc=domain"|dsquery computer -name %computername%

But I can't do that, because Windows 2000 doesn't include the dsquery & dsmove commands. I tried copying the dsmove.exe and dsquery.exe (even dsquery.dll) commands from XP to these boxes, they don't run on W2K.

So I guess I need to do this with a VBScript, instead. I've seen plenty of scripts about moving objects around AD, but these examples all assume you know the originating computer name and OU, as well as the target OU. Well, I only know the target OU: I don't know the originating computer name or OU.

None of the example scripts I've seen showed me how to simply move this computer to a different OU. It's always examples of querying the OU for its entire contents, or moving an entire OU. I just want to move one computer at a time.

The plan is to give this script to tech support personnel, who will logon to a computer with their domain credentials (they have rights to move computer objects around AD), and run the script. The script would then move that computer -- the computer the tech is currently using -- from wherever it happens to be, (we don't know where that is) to a specific OU, for remediation and management.

Thanks in advance for your time and help!

Ralph S. Thomas
Wiseman82
New Member
New Member
Posts:33

--
20 Aug 2006 11:00 AM
I've added this script to my website:

Hope this helps,

David
ralphsthomas
Rookie
Rookie
Posts:2

--
21 Aug 2006 06:52 AM
Thank you very much! I'll give it a try!

Ralph S. Thomas
nickraj12
Rookie
Rookie
Posts:1

--
14 Feb 2007 07:47 AM
Can you help me! I need to have a script that will get the MAC address of the computer( which is already jioned to local domain) , Then compare that mac with the existsing list of alll the mac addresses, this will give me the OU that , the computer should join , and then rename the computer . I have got many script that join to the domain .. but how to join to the OU . I have no idea ?
sobersage
Rookie
Rookie
Posts:1

--
10 Jan 2008 01:04 PM
Hello I know that this may not be exactly what you are looking for but here is a script that looks to a text file and reads the host names searches for them in AD then moves them to a OU that you specify in the VBScript.

So here is code below.


Start Code:

'######################################################################
'Active Directory Computer Object Search and Move Script
'Description:
' This script connects to Active Directory, searches for the computer object that is specified in the hostnames.txt file
' that resides in the same folder as the script, then places it into a new OU. You will need Domain Admin rights to run
' this script.
'Variables to change
' Line 19: Change hostnames.txt to your text file or just use the text file with your host names
' Line 41: Change the LDAP:// line to reflect where in your domain you wish the Computer Object to be moved to
' Line 54: Change the LDAP://dc=domain to dc=yourdomain,dc=com (net, us, etc)
'
'Created  01/09/2008
'#########################################################################

Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Specify the text file you wish to read for hostnames
Set objTextFile = objFSO.OpenTextFile ("hostnames.txt", ForReading) 'Only change the hostnames.txt name nothing else.
Do Until objTextFile.AtEndOfStream
' ******************** '
' * * '
' * Read in Hostname * '
' * * '
' ******************** '
strNextLine = objTextFile.Readline
arrServiceList = Split(strNextLine , ",")


' *********************** '
' * * '
' * Find hostname in AD * '
' * * '
' *********************** '

On Error Resume Next

Const ADS_SCOPE_SUBTREE = 2

'Change the LDAP:// to reflect your environment (the OU that it will end up in will come first, then what OUs before it ending with the domain
Set objNewOU = GetObject("LDAP://ou=dept,ou=computers,ou=site,ou=company,dc=domain,dc=com")

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection

objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

'Change the LDAP://dc=domain to dc=yourdomain,dc=com (net, us, etc)
objCommand.CommandText = _
"SELECT ADsPath FROM 'LDAP://dc=domain,dc=com' WHERE objectCategory='computer' " & _
"AND name='" & arrServiceList(0) & "'"
Set objRecordSet = objCommand.Execute

objRecordSet.MoveFirst

' ******************** '
' * * '
' * display pathname * '
' * * '
' ******************** '
'Do Until objRecordSet.EOF
'Wscript.Echo objRecordSet.Fields("ADsPath").Value
'objRecordSet.MoveNext
'Loop

' ****************** '
' * * '
' * Move to new OU * '
' * * '
' ****************** '
Do Until objRecordSet.EOF
strADsPath = objRecordSet.Fields("ADsPath").Value
Set objMoveComputer = objNewOU.MoveHere (strADsPath, vbNullString)
objRecordSet.MoveNext
Loop










Loop

End Code
You are not authorized to post a reply.

Acceptable Use Policy

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.