• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1871
  • Last Modified:

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.
0
caddy11
Asked:
caddy11
1 Solution
 
JesterTooCommented:
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:

          http://www.serverwatch.com/tutorials/article.php/1474241

Good luck,
Lynn


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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now