Creating the NTGroupManagement Class Module

   

Creating the NTGroupManagement Class Module

In this section, you continue the creation of the NTAdmin.DLL COM server application you started in Chapter 3.

Just as in all previous chapters, manipulation of the IADsGroup interface (and one method of the IADsUser interface) is handled by a class module within an ActiveX DLL.

Example 5.1 Creating NTAdmin.DLL: Group Management Functions
  1. Open the NTAdmin ActiveX DLL Visual Basic project that was started in Chapter 3. You can also download the project from http://www.newriders.com/adsi.

  2. If you are adding to the NTAdmin project, add a new class module to the project. If this is a new project, make sure to set a reference to Active DS Type Library.

  3. Name the new module NTGroupManagement .

  4. Enter the following code into the General Declarations section of the class module:

      Public Function ManageGroup(ByVal TargetGroupDomain As String, ByVal TargetGroupName As graphics/ccc.gif String, ByVal TargetUserDomain As String, ByVal TargetUserName As String, ByVal Action As graphics/ccc.gif String) As Boolean   Dim Group As IADsGroup   Dim User As IADsUser   Set User = GetObject("WinNT://"&TargetUserDomain&"/"&TargetUserName& ",user")   Set Group = GetObject("WinNT://"&TargetGroupDomain&"/"&TargetGroupName & ",group")   Select Case UCase(Action)   Case "ADD"   Group.Add (User.ADsPath)   Case "REMOVE"   Group.Remove (User.ADsPath)   End Select     Group.SetInfo   If Err.Number = 0 Then ManageGroup = True Else ManageGroup = False   End Function   Public Function EnumerateGroupMembers(ByVal TargetGroupDomain As String, ByVal graphics/ccc.gif TargetGroupName As String) As Variant   On Error Resume Next   Dim Group As IADsGroup   Dim NewElement() As Variant   Dim i As Long   Set Group = GetObject("WinNT://"&TargetGroupDomain&"/"&TargetGroupName & ",group")   For Each Member In Group.Members   i = UBound(NewElement) + 1   ReDim Preserve NewElement(i)   NewElement(i) = Member.Name   Next   EnumerateGroupMembers = NewElement   End Function   Public Function VerifyGroupMembership(ByVal TargetGroupDomain As String, ByVal graphics/ccc.gif TargetGroupName As String, ByVal TargetUserDomain As String, ByVal TargetUserName As graphics/ccc.gif String) As Boolean   Dim Group As IADsGroup   Dim User As IADsUser   Set User = GetObject("WinNT://"&TargetUserDomain&"/"&TargetUserName& ",user")   Set Group = GetObject("WinNT://"&TargetGroupDomain&"/"&TargetGroupName & ",group")   VerifyGroupMembership = Group.IsMember(User.ADsPath)   End Function   Public Function QueryGroupDescription(ByVal TargetGroupDomain As String, ByVal graphics/ccc.gif TargetGroupName As String) As String   Dim Group As IADsGroup   Set Group = GetObject("WinNT://"&TargetGroupDomain&"/"&TargetGroupName & ",group")   QueryGroupDescription = Group.Description   End Function   Public Function SetGroupDescription(ByVal TargetGroupDomain As String, ByVal graphics/ccc.gif TargetGroupName As String, ByVal NewDescription As String) As Boolean   Dim Group As IADsGroup   Set Group = GetObject("WinNT://"&TargetGroupDomain&"/"&TargetGroupName & ",group")   Group.Description = NewDescription   Group.SetInfo   If Err.Number = 0 Then SetGroupDescription = True Else SetGroupDescription = False   End Function  
  5. Compile the code as NTAdmin.DLL .

  6. Save and close the NTAdmin project.

Tip

You can download the Visual Basic 6.0 project or a pre-compiled version of NTAdmin.DLL from http://www.newriders.com/adsi


Using the Functions in NTGroupManagement

With the NTGroupManagement class module created, you can access this function from any programming language that supports OLE automation, including Visual Basic, VBScript, and JavaScript.

Tip

To instantiate the object, follow the appropriate syntax found in Chapter 3. Substitute the NTGroupManagement class name where necessary .


Use Table 5.2 to help you use the proper syntax for each of the methods of the NTGroupManagement interface.

Table 5.2. NTGroupManagement Method Syntax.
Action Syntax
Add a member to a group
 Debug.Print ManageGroup("Group_Target_Domain, graphics/ccc.gif "Target_Group_Name", "Target_UserDomain", graphics/ccc.gif "Target_UserName", "Add") 
Remove a member from a group
 Debug.Print ManageGroup("Group_Target_Domain", graphics/ccc.gif "Target_Group_Name", "Target_UserDomain", graphics/ccc.gif "Target_UserName", "Remove") 
Enumerate all users in a group
 For Each Item In graphics/ccc.gif EnumerateGroupMembers("Group_Target_ Domain", graphics/ccc.gif "Target_Group_Name") Debug.Print Item Next 
Verify user membership in a given group
 Debug.Print VerifyGroupMembership("Group_Target_ graphics/ccc.gif Domain", "Target_Group_Name", "Target_UserDomain", graphics/ccc.gif "Target_UserName") 
Query the description for a given group
 Debug.Print QueryGroupDescription("Group_Target_ graphics/ccc.gif Domain", "Target_Group_Name') 
Set a new description for agiven group
 Debug.Print SetGroupDescription("Group_Target_ graphics/ccc.gif Domain", "Target_Group_Name", graphics/ccc.gif "New_Group_Description") 

   
Top


Windows NT. 2000 ADSI Scripting for System Administration
Windows NT/2000 ADSI Scripting for System Administration
ISBN: 1578702194
EAN: 2147483647
Year: 2000
Pages: 194
Authors: Thomas Eck

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net