The Active Directory Services Interface tool (ADSI) provides a single consistent set of interfaces that can be called in scripts using Microsoft Windows Script Host (WSH), or other scripting languages (VBScript and JScript are supported natively).
This article demonstrates how an administrator can use ADSI to script the
creation, deletion, and management of groups and group membership within Active Directory.
The following sample scripts are provided for demonstration purposes only.
NOTE: These scripts require the appropriate security context to operate. They must be run from a session in which the logged-on user has permission to create a group object, delete a group object, and add or remove members from groups.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''
'' GROUPUSERADD.VBS
''
'' Adds the specified user to the specified group
''
'' usage: CreateGroup PROVIDER: GROUPSUFFIX USERSUFFIX ADMIN PASSWORD LOGFILE
''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Option Explicit
Public Const ForReading = 1
Public Const ForWriting = 2
Public Const ForAppending = 8
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'' Sub LogMessage() - writes a message to the screen and logfile
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub LogMessage(fsOut, Msg)
WScript.Echo msg
fsOut.WriteLine msg
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'' Sub BailOnFailure() - writes a message to the screen and logfile
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub BailOnFailure(fsOut, ErrNum, ErrText)
LogMessage fsOut, "GroupUserAdd: FAIL Error 0x" & Hex(ErrNum) & " " & ErrText
LogMessage fsOut, " Provider = " & szProvider
LogMessage fsOut, " Group = " & szGroupPath
LogMessage fsOut, " User = " & szUserPath
LogMessage fsOut, " Admin = " & szAdmin
LogMessage fsOut, " Password = " + Chr(34) + szPassword + Chr(34)
fsOut.Close
WScript.Quit
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'' main()
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim oArgs
Dim oOpenDsObject
Dim oObject
Dim oGroup
Dim szProvider
Dim szGroupPath
Dim szUserPath
Dim szAdmin
Dim szPassword
Dim szLogFile
Dim fs
Dim fsOut
On Error Resume Next
'Stop
Set oArgs = WScript.Arguments
If (oArgs.Count <> 6) Then
WScript.Echo "usage: GroupUserAdd <Provider:> <GroupSuffix> <UserSuffix> <Admin> <Password> <logfile>"
'"For example, GroupUserAdd LDAP: CN=VBS_GROUP,OU=VBS_ORGUNIT,O=VBS_ORG,DC=NW01T1DOM,DC=NTDEV,DC=MICROSOFT,DC=COM CN=Administrator,CN=Users,DC=NW01T1DOM,DC=NTDEV,DC=MICROSOFT,DC=COM " + Chr$(34) + Chr$(34) + " grpusrad.log"
Else
szProvider = oArgs(0)
szGroupPath = oArgs(0) + "//" + oArgs(1)
szUserPath = oArgs(0) + "//" + oArgs(2)
szAdmin = oArgs(3)
szPassword = oArgs(4)
szLogfile = oArgs(5)
Set fs = CreateObject("Scripting.FileSystemObject")
Set fsOut = fs.OpenTextFile(szLogFile, ForAppending, True)
Set oOpenDSObject = GetObject(szProvider)
Set oGroup = oOpenDSObject.OpenDSObject(szGroupPath, szAdmin, szPassword, 1)
If (Err.Number <> 0) Then
BailOnFailure fsOut, Err.Number, "binding to group object"
End If
'Stop
oGroup.Add szUserPath
If (Err.Number <> 0) Then
BailOnFailure fsOut, Err.Number, "invoking Add() method"
End If
LogMessage fsOut, "GroupUserAdd: PASS"
fsOut.Close
WScript.Quit(Err.Number)
End If
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''
'' GROUPUSERemove.VBS
''
''Remove the specified user from the specified group
''
'' usage: CreateGroup PROVIDER: GROUPSUFFIX USERSUFFIX ADMIN PASSWORD LOGFILE
''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Option Explicit
Public Const ForReading = 1
Public Const ForWriting = 2
Public Const ForAppending = 8
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'' Sub LogMessage() - writes a message to the screen and logfile
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub LogMessage(fsOut, Msg)
WScript.Echo msg
fsOut.WriteLine msg
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'' Sub BailOnFailure() - writes a message to the screen and logfile
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub BailOnFailure(fsOut, ErrNum, ErrText)
LogMessage fsOut, "GroupUserRemove: FAIL Error 0x" & Hex(ErrNum) & " " & ErrText
LogMessage fsOut, " Provider = " & szProvider
LogMessage fsOut, " Group = " & szGroupPath
LogMessage fsOut, " User = " & szUserPath
LogMessage fsOut, " Admin = " & szAdmin
LogMessage fsOut, " Password = " + Chr(34) + szPassword + Chr(34)
fsOut.Close
WScript.Quit
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'' main()
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim oArgs
Dim oOpenDsObject
Dim oObject
Dim oGroup
Dim szProvider
Dim szGroupPath
Dim szUserPath
Dim szAdmin
Dim szPassword
Dim szLogFile
Dim fs
Dim fsOut
On Error Resume Next
'Stop
Set oArgs = WScript.Arguments
If (oArgs.Count <> 6) Then
WScript.Echo "usage: GroupUserAdd <Provider:> <GroupSuffix> <UserSuffix> <Admin> <Password> <logfile>"
'"For example, GroupUserAdd LDAP: CN=VBS_GROUP,OU=VBS_ORGUNIT,O=VBS_ORG,DC=NW01T1DOM,DC=NTDEV,DC=MICROSOFT,DC=COM ADSGROUP CN=Administrator,CN=Users,DC=NW01T1DOM,DC=NTDEV,DC=MICROSOFT,DC=COM " + Chr$(34) + Chr$(34) + " grpusrad.log"
Else
szProvider = oArgs(0)
szGroupPath = oArgs(0) + "//" + oArgs(1)
szUserPath = oArgs(0) + "//" + oArgs(2)
szAdmin = oArgs(3)
szPassword = oArgs(4)
szLogfile = oArgs(5)
Set fs = CreateObject("Scripting.FileSystemObject")
Set fsOut = fs.OpenTextFile(szLogFile, ForAppending, True)
Set oOpenDSObject = GetObject(szProvider)
Set oGroup = oOpenDSObject.OpenDSObject(szGroupPath, szAdmin, szPassword, 1)
If (Err.Number <> 0) Then
BailOnFailure fsOut, Err.Number, "binding to group object"
End If
'Stop
oGroup.Remove szUserPath
If (Err.Number <> 0) Then
BailOnFailure fsOut, Err.Number, "invoking Remove() method"
End If
LogMessage fsOut, "GroupUserRemove: PASS"
fsOut.Close
WScript.Quit(Err.Number)
End If