The following scripts (VBS) allows play with Active Directory.
It details a way to:
- Create an Organizational Unit
- Create a Group in an Organizational Unit
- Rename a Group
- List all Groups in an Organizational Unit
- Create a User in an Organization Unit and assign it to a Group
- List all members in a Group
- Delete Users and Groups
- Delete Organizational Unit
1. CreateOU.vbs
- Connection to the domain test.netsrv.info
- Creation of the Organizational Unit (OU) CH_DAMIEN
- Creation of the sub-OU CH01
- Creation of the 2 sub-OU GRP and USR
- Creation of the sub-OU CH01
' That script allows to create the OU "CH_DAMIEN", then the sub-OU "CH01", then the sub-sub-OU "GRP" and "USR"
' Connection to the domain connected to the machine
Set oRoot = GetObject("LDAP://rootDSE")
Set oDomain = GetObject("LDAP://" & oRoot.Get("defaultNamingContext"))
' To define manually the connection (for example the domain "test.netsrv.info")
' Set oDomain = GetObject("LDAP://DC=test,DC=netsrv,DC=info")
Set oCH=oDomain.Create("organizationalUnit", "ou=CH_DAMIEN")
oCH.Put "Description", "Damien Test OU"
oCH.SetInfo
Set oCH01=oCH.Create("organizationalUnit", "ou=CH01")
oCH01.Put "Description", "Damien Test CH01"
oCH01.SetInfo
Set oGRP=oCH01.Create("organizationalUnit", "ou=GRP")
oGRP.Put "Description", "Damien Test GRP"
oGRP.SetInfo
Set oUSR=oCH01.Create("organizationalUnit", "ou=USR")
oUSR.Put "Description", "Damien Test USR"
oUSR.SetInfo
.
.
2. AddGroup.vbs
- Creation of the group « DemoGroup » in the OU “GRP”
' That script allows to create the group "DemoGroup" in the OU "CH_DAMIEN/CH01/GRP" of the domain "test.netsrv.info"
On Error Resume Next
' Connection to the OU "CH_DAMIEN/CH01/GRP" of the domain "test.netsrv.info"
Set oGRP = GetObject("LDAP://OU=GRP,OU=CH01,OU=CH_DAMIEN,DC=test,DC=netsrv,DC=info")
' Adding the group "DemoGroup" in the OU
Set oGroup = oGRP.Create("Group", "CN=DemoGroup")
oGroup.Put "sAMAccountName", "DemoGroup"
oGroup.Put "Description", "Demonstration Group"
oGroup.SetInfo
.
.
3. RenameGroup.vbs
- Rename the group « DemoGroup » : « NewDemoGroup »
' That script allows renaming the group "DemoGroup" in "NewDemoGroup" in the OU "CH_DAMIEN/CH01/GRP" of the domain "test.netsrv.info"
On Error Resume Next
' Connection to the OU "CH_DAMIEN/CH01/GRP" of the domain "test.netsrv.info"
Set oOU = GetObject("LDAP://OU=GRP,OU=CH01,OU=CH_DAMIEN,DC=tets,DC=netsrv,DC=info")
' To rename the group, we move it giving it a new name
oOU.MoveHere "LDAP://CN=DemoGroup,OU=GRP,OU=CH01,OU=CH_DAMIEN,DC=uat01,DC=netsvc,DC=info", "CN=NewDemoGroup"
.
.
4. ListGroups.vbs
- Listing of all groups in the OU « GRP »
' That script allows to list groups in the OU "CH_DAMIEN/CH01/GRP" of the domain "test.netsrv.info"
On Error Resume Next
' Connection to the OU "CH_DAMIEN/CH01/GRP" of the domain "test.netsrv.info"
Set objCH = GetObject("LDAP://OU=GRP,OU=CH01,OU=CH_DAMIEN,DC=test,DC=netsrv,DC=info")
' Display of the group list
For Each objGroup in objCH
msgbox(objGroup.CN)
Next
.
.
5. AddUser.vbs
- Creation of the user « DemoUser » in the OU « USR »
- Assignment of the user « DemoUser » as member of the group « NewDemoGroup »
' That script allows to create the User "DemoUser" in the OU "CH_DAMIEN/CH01/USR" of the domain "test.netsrv.info", then to assign "DemoUser" as member of the group "NewDemoGroup"
' Connection to the OU "CH_DAMIEN/CH01/USR" of the domain "test.netsrv.info"
Set oUSR = GetObject("LDAP://OU=USR,OU=CH01,OU=CH_DAMIEN,DC=test,DC=netsrv,DC=info")
' Adding "DemoUser" in the OU
Set oUser = oUSR.Create("User", "CN=DemoUser")
oUser.Put "sAMAccountName", "DemoUser"
oUser.Put "Description", "Demonstration User"
oUser.SetInfo
oUser.SetPassword "qwertz00"
oUser.AccountDisabled = False
oUser.SetInfo
' Connection to the group "NewDemoGroup" in the OU "CH_DAMIEN/CH01/GRP"
Set oGroup = GetObject("LDAP://CN=NewDemoGroup,OU=GRP,OU=CH01,OU=CH_DAMIEN,DC=uat01,DC=netsvc,DC=info")
' Assign "DemoUser" as member of the group "NewDemoGroup"
oGroup.Add oUser.ADSPath
.
.
6. ListGroupMembers.vbs
- Listing of all members of the group « NewDemoGroup »
' That script allows to list members of the group "NewDemoGroup" of the OU "CH_DAMIEN/CH01/GRP" of the domain "test.netsrv.info"
On Error Resume Next
' Connection to the group "NewDemoGroup" of the OU "CH_DAMIEN/CH01/GRP" of the domain "test.netsrv.info"
Set objGRP = GetObject("LDAP://CN=NewDemoGroup,OU=GRP,OU=CH01,OU=CH_DAMIEN,DC=test,DC=netsrv,DC=info")
objGRP.GetInfo
' list the members of the group "NewDemoGroup"(we display only the CN)
arrMemberOf = objGRP.GetEx("member")
For Each strMember in arrMemberOf
PosEqual = InStr(1, strMember, "=", 1)
strMemberUser = Mid(strMember, (PosEqual+1))
PosComa = InStr(1, strMemberUser, ",", 1)
strMemberUser = Left(strMemberUser, PosComa - 1)
MsgBox(strMemberUser)
Next
.
7. DeleteUserAndGroup.vbs
- Delete the user « DemoUser »
- Delete the group « NewDemoGroup »
' That script allows to delete the User "DemoUser" from the OU "CH_DAMIEN/CH01/USR" of the domain "test.netsrv.info"
' Then deleting the group "NewDemoGroup" from the OU "CH_DAMIEN/CH01/GRP"
On Error Resume Next
' Deleting "DemoUser" from the OU "CH_DAMIEN/CH01/USR" of the domain "test.netsrv.info"
Set oUSR = GetObject("LDAP://OU=USR,OU=CH01,OU=CH_DAMIEN,DC=uat01,DC=netsvc,DC=info")
oUSR.Delete "User", "CN=DemoUser"
' Deleting "NewDemoGroup" from OU "CH_DAMIEN/CH01/GRP" of the domain "test.netsrv.info"
Set oGRP = GetObject("LDAP://OU=GRP,OU=CH01,OU=CH_DAMIEN,DC=uat01,DC=netsvc,DC=info")
oGRP.Delete "Group", "CN=NewDemoGroup"
.
.
8. DeletOU.vbs
- Delete the ou « USR »
- Delete the ou « GRP »
- Delete the ou « CH01 »
- Delete the ou « CH_DAMIEN »
' That script allows to delete the OU CH_DAMIEN and its sub-OU from the domain "test.netsrv.info"
On Error Resume Next
'Delete OU USR
Set oDomain = GetObject("LDAP://OU=CH01,OU=CH_DAMIEN,DC=test,DC=netsrv,DC=info")
oDomain.Delete "organizationalUnit", "OU=USR"
'Delete OU GRP
Set oDomain = GetObject("LDAP://OU=CH01,OU=CH_DAMIEN,DC=test,DC=netsrv,DC=info")
oDomain.Delete "organizationalUnit", "OU=GRP"
'Delete OU CH01
Set oDomain = GetObject("LDAP://OU=CH_DAMIEN,DC=test,DC=netsrv,DC=info")
oDomain.Delete "organizationalUnit", "OU=CH01"
'Delete OU CH_DAMIEN
Set oDomain = GetObject("LDAP://DC=test,DC=netsrv,DC=info")
oDomain.Delete "organizationalUnit", "OU=CH_DAMIEN"






