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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AD Computer move automation

Status
Not open for further replies.

ShiroFox

IS-IT--Management
Feb 15, 2009
2
ZA
Hi

I have a bit of a problem. I need to move all the computers in the default computers container in AD to an OU according to witch subnet they are located in in AD sites and services or according to IP address.

At the moment I run a script to extract the computer names out of the default container and dump the results in a txt file. I then use FastResolver to get the ip addresses of the computers in the txt file and then save it in a .CSV. The problem is I have to manually check each ip with the scope in sites and services and move it to a txt according to witch site the subnet is assigned to. I then run a script that moves the computers from the txt file to the correct OU in AD.

Is there any way I can automate this process. Perhaps by making the script take a pc name from the container and then ping it. Then take the ip it got and convert it to a subnet address. Take the subnet address and use something like a IF statement to move it to the correct OU in AD.

If its possible to get the same info you would get using

Set objADSysInfo = CreateObject("ADSystemInfo")

WScript.Echo "Current site name: " & objADSysInfo.SiteName

From a remote computer that will help a lot as there is currently 271 subnets split over 25 sites and that would mean there will be at least 271 subnet entries in the script. Perhaps there is even a better way to do this but I’m a novice when it comes to VBS

Ill post the two scripts I currently use in my next post.
 
Script to dump pcs to txt:

Const ADS_SCOPE_SUBTREE = 2
Const ForAppending = 8

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Dim outFile
Set outFile = fso_OpenTextFile("Computers_dump.txt", ForAppending, True)
Set objADSysInfo = CreateObject("ADSystemInfo")

Dim cn
Set cn = CreateObject("ADODB.Connection")
cn.Provider = "ADsDSOObject"
cn.Open "Active Directory Provider"
Dim cmd
Set cmd = CreateObject("ADODB.Command")
Set cmd.ActiveConnection = cn
Dim ou
ou = "CN=computers,DC=domain,DC=net"
cmd.CommandText = "SELECT name " & _
"FROM 'LDAP://" & ou & "' " & _
"WHERE objectClass='computer' " & _
"ORDER BY name"
cmd.Properties("Page Size") = 1000
cmd.Properties("Searchscope") = ADS_SCOPE_SUBTREE

Dim rs
Set rs = cmd.Execute
rs.MoveFirst

Do Until rs.EOF
outFile.WriteLine rs(0)
rs.MoveNext
Loop

outFile.Close
Set outFile = Nothing
Set fso = Nothing


Script to move computers from txt:

Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Specify the text file you wish to read for hostnames
Set objTextFile = objFSO.OpenTextFile ("Computers_Head_Office.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=Computers HO,ou=Head Office,dc=domain,dc=net")

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=AngloPlat,dc=net' 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


Any help would be appreciated as Im losing my mind doing this over and over on a weekly basis
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top