Creating AD objects with VBScript

Ö

Örjan Johansson

Hi all!

I've been studying a bunch of scripts to aid in creating a rather complex AD
structure, and I've seen several examples of creating both OU's and users.
However, all of them create OU's at the root level, and users in that OU.

For example:
##Script
Set oRoot = GetObject("LDAP://rootDSE")
Set oDomain = GetObject("LDAP://" &
oRoot.Get("defaultNamingContext"))

Set oOU=oDomain.Create("organizationalUnit",
"ou=Demo OU")
oOU.Put "Description", "Demonstration OU"
oOU.SetInfo

Set oGroup = oOU.Create("Group", "cn=Demo
Group")

oGroup.Put "sAMAccountName", "DemoGroup"

oGroup.Put "Description", "Demonstration
Group"
oGroup.SetInfo

Set oUser = oOU.Create("User", "cn=Demo User")

oUser.Put "sAMAccountName", "DemoUser"

oUser.Put "Description", "Demonstration User"

oUser.SetInfo

oUser.SetPassword "qW5rty"

oUser.AccountDisabled = False
oUser.SetInfo

oGroup.Add oUser.ADSPath
#/script

I want to create OU's several levels deep, for instance an OU called
Mycompany at the root level, an OU called Stockholm in that OU, another one
called Gothenbourg in the same OU, an OU called Admin in each of these, an
OU called users in each of these etc etc... (hope I'm not losing you guys).

Can anyone point me to docs or scripts describing creating more complex
structures like this?

Any input extremely appreciated!

TIA,
Örjan
 
M

Matjaz Ladava [MVP]

change

Set oDomain = GetObject("LDAP://" &
oRoot.Get("defaultNamingContext"))

into

Set oDomain = GetObject("LDAP://ou=secondou,ou=firstou," &
oRoot.Get("defaultNamingContext"))

This will connect to second ou below first ou (they must already exist). It
is just a matter of LDAP path.

--

Regards

Matjaz Ladava, MCSA, MCSE, MCT, MVP
Microsoft MVP Windows Server - Active Directory
(e-mail address removed), (e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top