VBScript to Move AD Computers

Posted on 2006-05-18
Last Modified: 2008-04-11
What I am after is a script to move computers from one OU to another OU.  Why?  Well all new computers go to an "Unassigned" OU when they join the domain, then we have separate OU's for operating systems (2k and XP) so the script needs to determine the operating system then move to the correct OU for that operating system.  However it should not move servers.

I have searched the WEB and have created the below script, but it doesn't seem to be working correctly....if anyone can help it will be appreciated

Option Explicit

Dim objRootDSE, strDNSDomain, objCommand, objConnection
Dim strBase, strFilter, strAttributes, strQuery, objRecordSet
Dim strDN, strOS, objW2kOU, objXPOU

' Bind to target W2k and XP OU containers.
Set objW2kOU = GetObject("LDAP://ou=2k,ou=workstations,ou=Computers,ou=OU,dc=MY,dc=DOMAIN,dc=COM")
Set objXPOU = GetObject("LDAP://ou=XP,ou=workstations,ou=Computers,ou=OU,dc=MY,dc=DOMAIN,dc=COM")

' Determine DNS domain name.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")

' Use ADO to search Active Directory.
Set objCommand = CreateObject("ADODB.Command")
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnection = objConnection
strBase = "<LDAP://" & strDNSDomain & ">"

' Filter on all computer objects.
strFilter = "(objectCategory=computer)"
' Retrieve distinguishedName and operatingSystem attributes.
strAttributes = "distinguishedName,operatingSystem"
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
objCommand.CommandText = strQuery
objCommand.Properties("Page Size") = 100
objCommand.Properties("Timeout") = 30
objCommand.Properties("Cache Results") = False
Set objRecordSet = objCommand.Execute

' Loop through all computer objects.
Do Until objRecordSet.EOF
' Make strings lower case.
strDN = LCase(objRecordSet.Fields("distinguishedName").Value)
strOS = LCase(objRecordset.Fields("operatingsystem").Value)
' Check if computer object in cn=computers container.
If (InStr(strDN, "OU=Unassigned,OU=Computers,OU=OU,DC=york,MY=gov,DOMAIN=COM")) Then
' Check OS, skip servers.
If strOS = "windows 2000 Professional" Then
Set objMoveComputer = objW2kOU.MoveHere("LDAP://" & strDN, vbNullString)
ElseIf strOS = "Windows XP Professional" Then
Set objMoveComputer = objXPOU.MoveHere("LDAP://" & strDN, vbNullString)
End If
End If
' Move to the next computer object.

' Clean up.
Set objRootDSE = Nothing
Set objCommand = Nothing
Set objConnection = Nothing
Set objRecordSet = Nothing
Set objW2kOU = Nothing
Set objXPOU = Nothing
Question by:jarjarbinx1979
    LVL 16

    Accepted Solution

    I think its because you forced strDN and strOU to be lower case using LCASE and when you are doing the INSTR function
    you are comparing it to a string that has uppercase letters
    LVL 16

    Expert Comment

    ooops there is no strOU
    meant to say strDN and strOS
    LVL 16

    Expert Comment

    I'm pretty sure that was it.
    I can't see any other problems with your script.

    It fails for me as well, until I change the INSTR comparison to be lower case
    LVL 2

    Expert Comment

    How should that INSTR syntax look when forced to lowercase?

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Introduction While answering a recent question ( in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
    Article by: Martin
    Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now