Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Moving computer accounts using VBScript

Status
Not open for further replies.

mikeyb79

IS-IT--Management
Apr 15, 2008
2
US
Hi all, I have a question about working with computer accounts. I've been trying to piece something together, I almost never need to do any scripting but am trying to automate a regular task that is very time-consuming.

I get a list of computer names in a text file, normally about 500 or so, that need to be moved in bulk to a specific OU.

The problem is, those computer names exist in different OU's across the domain. So to manually move these accounts from their current locations is very time consuming.

I have a bunch of code snippets but I am totally lost here! What I know is the computer names (in a TXT file conveniently called "computernames.txt", but it does not include the full distinguished name), the domain they all reside in, and the OU I want to move them all too.

What is the best way to proceed?

This is what I have so far but I haven't even run it yet:

On Error Resume Next

Const ADS_SCOPE_SUBTREE = 2
Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Computernames.txt", ForReading)
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

Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine

objCommand.CommandText = _
"SELECT ADsPath FROM 'LDAP://dc=test,dc=domain,dc=com' WHERE objectCategory='computer' " & _
"AND name='"strline"'"
Set objRecordSet = objCommand.Execute

objRecordSet.MoveFirst
Do Until objRecordSet.EOF
strADsPath = objRecordSet.Fields("ADsPath").Value
Set objOU = GetObject("LDAP://OU=04 - Apr,OU=Computer Account Maintenance,OU=Workstations,DC=test,DC=domain,DC=com")
intReturn = objOU.MoveHere(strADsPath, vbNullString)
objRecordSet.MoveNext
Loop

Loop

objFile.Close
 
I suggest making a test OU and test machine and place the test machine in that OU. Remove all the machines from your computernames.txt file except for the test machine. Test and see if you receive any errors.
 
I tried the script center link, my script is mostly pieced together from it and a few others. Problem is when I run the script, I see the Microsoft Windows Script Host message, then it skips a line and returns to the prompt, no errors. But, it doesn't do anything, even if I specify the full computer name in the script.
 
no errors
Comment out the On Error Resume Next line to discover what really happen.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
i would suggest its not efficient to execute a objCommand.Execute for every line in your file
i would also suggest there is no point moving a machine into an OU when possibly it is already residing there

name='"strline"'", should perhaps read '" & strLine & "'"

On Error Resume Next is for girls ;-)
 
Try this.... Uses name translate instead of ADO, which should be faster.

I haven't tested it yet, but it should be close.

Code:
Option Explicit

Dim objFSO, objFile, objNetwork, objTrans, objDomain, strCompName,  strCompDN, objOU

Const strDestOU = "OU=04 - Apr,OU=Computer Account Maintenance,OU=Workstations,DC=test,DC=domain,DC=com"

Const ADS_SCOPE_SUBTREE = 2
Const ForReading = 1

' Constants for the NameTranslate object.
Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Computernames.txt", ForReading)
Set objNetwork = CreateObject("WScript.Network")

Set objTrans = CreateObject("NameTranslate")
Set objDomain = getObject("LDAP://rootDse")

Do Until objFile.AtEndOfStream
	strCompName = objFile.ReadLine

	objTrans.Init ADS_NAME_INITTYPE_GC, ""
	objTrans.Set ADS_NAME_TYPE_NT4, objNetwork.UserDomain & "\" & strCompName & "$"
	strCompDN = objTrans.Get(ADS_NAME_TYPE_1779)
	
	If InStr(1, strCompDN, strDestOU, vbtextcompare) = 0 Then
		Set objOU = GetObject("LDAP://" & strDestOU)
		objOU.MoveHere "LDAP://" & strCompDN, vbNullString
	End If

Loop

PSC

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top