Need VB Script to Move Computer to AD Container based on User CN

Posted on 2006-05-26
Last Modified: 2012-08-14
We are about to do a large rollout of new computers and in order to help automate things I'm trying to write a script that will automatically put the computer into the right container after it has joined the domain based on the currently logged in user's ID.

A typical user CN looks like this:

cn=userid, OU=USERS,OU=IT, OU=NY,DC=domain,DC=com
cn=userid2, OU=USERS,OU=HR, OU=LA, DC=domain,DC=com

There is a corresponding CN for computers that would look like this:

cn=computer, OU=COMPUTERS, OU=IT, OU=NY,DC=domain,DC=com
cn=computer2, OU=COMPUTERS, OU=HR, OU=LA,DC=domain,DC=com

Basically I'm looking for a script that would move the current hostname into the right container based on the cn of the currently logged in user. The only difference is the OU at the end (USERS vs COMPUTERS).

I know the solution will involve the MoveHere command to some extent along the lines of something like the below.

Set objNewOU = GetObject("LDAP://" & strDestinationOU)
   Set objMoveComputer = objNewOU.MoveHere _
   ("LDAP://" & strComputer & strSourceOU, strComputer)

I'll need a full working vbs script.... Thanks in advance.
Question by:caddy11
    1 Comment
    LVL 20

    Accepted Solution

    This script will accomplish that task.  However, it should be installed as a GPO login script in order to have the necessary permissions to modify AD.  If you need info regarding setting a login script via GPO then please refer to this link:


    Good luck,

    Option Explicit

    Dim oSysInfo
    Dim sUserDN
    Dim sComputerDN
    Dim n
    Dim sTemp
    Dim sPrefix, sSuffix1, sSuffix2

    '-- find the distinguished name of the user who is logged in
    Set oSysInfo = CreateObject("ADSystemInfo")
    sUserDN = oSysInfo.UserName
    WScript.Echo sUserDN

    '-- find the distinguished name of the user's computer
    sComputerDN = oSysInfo.ComputerName
    WScript.Echo sComputerDN

    '-- analyze the two DN's to see if the computer needs to be
    '-- moved...
    n        = InStr(sUserDN,",")     '-- find first comma
    sTemp    = Mid(sUserDN,n+1)       '-- save remaining portion of DN
    n        = InStr(sTemp,",")       '-- find second comma
    sSuffix1 = Mid(sTemp,n+1)         '-- save remaining portion of DN

    n        = InStr(sComputerDN,",") '-- find first comma
    sPrefix  = Left(sComputerDN,n)    '-- save prefix
    sSuffix2 = Mid(sComputerDN,n+1)   '-- save suffix

    If UCase("CN=COMPUTERS," & sSuffix1) <> UCase(sSuffix2) Then
    End If

    Set oSysInfo = Nothing

    Sub MoveComputer()
    Dim sSource
    Dim sDestination
    Dim oContainer

       sSource        = "LDAP://" & sPrefix & sSuffix2
       sDestination   = "LDAP://CN=Computers," & sSuffix1
       Set oContainer = GetObject(sDestination)

    '''Wscript.Echo "will move " & sSource & " to here---> " & sDestination

       Call oContainer.MoveHere(sSource, vbNullString)

       Set oContainer to Nothing

    End Sub

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    substring method in java 1 59
    map interface methods 3 46
    NotAlone Challenge 20 65
    mapBully challenge 6 53
    Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
    Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

    729 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

    18 Experts available now in Live!

    Get 1:1 Help Now