Tuesday, January 25, 2011

Moving AD Objects Via VBS

Sometimes in AD reorganization of objects needs to occur to ease management. With a high number of objects, it might be more convientient to do this programatically. Lucily Microsoft has mad this task some what painless with VB Scripting. The process involves essentially three steps:

1. Create a scripting object to the new OU in AD where this object is to move

2. Create a scripting object to the original AD object

3. Use the MoveHere method of the new OU object to move the original object to that location

Example:

Say a computer account exists under the OU tree OU=Desktops,OU=Company,dc=location,dc=local. Let us assume that this computer account is called MYPC. Additionally, it is desired to move this account to the OU tree defined at OU=NewOU,dc=location,dc=local. Here's a sample scripts to do this for you

-------

szNewLoc="OU=NewOU,dc=location,dc=local"

szOldObj="CN=MYPC,OU=Desktops,OU=Company,dc=location,dc=local"

set oNewOU=GetObject("LDAP://" & szNewLoc)

set oCpu=GetObject("LDAP://" & szOldObj)

oNewOU.MoveHere oCpu.ADsPath, oCpu.Name

---

That it. Now lets decipher the script.

Lines 1 and 2 set string variables to the new OU and the original object

Line 3: creates a scripting object identifying the new storage location

Line 4. Creates a scripting object identifying the original object

Line 5: This does the actual move of the object. The MoveHere method, moves the source object (identified by the oCpu.Name property) from the original path (identified by the ADsPath property) the the location of the object oNewOU.

No comments: