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