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

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.
Who is Participating?
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.