Automating MTS Role Configuration

To configure a role a package and component, and assign administrative privileges to a user:
  1. Declare the objects that you will be using to configure a role for a specific component.
    Dim catalog As Object
    Dim packages As Object
    Dim pack As Object
    Dim comp As Object
    Dim newUser As Object
    Dim newRole As Object
    Dim componentsInPack As Object
    Dim RolesInPackage As Object
    Dim usersInRole As Object
    Dim rolesForComponent As Object
    Dim util As Object
  2. Use the On Error statement to handle run-time errors if a method returns a failure HRESULT. You can test and respond to MTS trappable errors using the On Error statement and the Err object.
    On Error GoTo failed
  3. Call the CreateObject method to instantiate the Catalog object. Retrieve the Packages collection by calling the GetCollection method. Then populate the Packages collections with data from the catalog.
    Set catalog = CreateObject("MTSAdmin.Catalog.1")
    Set packages = catalog.GetCollection("Packages")
    packages.Populate
  4. Enumerate through the Packages collection to look for the package named “My Package.” When “My Package” is located, call the GetCollection method to obtain the RolesInPackage collection. Add a new role to package using the Add method. Name the new role “R1,” and save changes to the collection.
    If pack.Name = "My Package" Then
     Set rolesInPack = packages.GetCollection("RolesInPackage", pack.Key)
                Set newRole = rolesInPack.Add
                newRole.Value("Name") = "R1"
                rolesInPack.savechanges
  5. Call the GetCollection method on the RolesInPackage collection to get the UsersInRole collection. Use the Add function to add an existing NT user to the role. Set the user name to “administrator.” Save changes to the UsersInRole collection.
    Set usersInRole = RolesInPackage.GetCollection("UsersInRole",                 newRole.Key)
                Set newUser = usersInRole.Add
                newUser.Value("User") = "administrator"
                usersInRole.savechanges
  6. Get the ComponentsInPackage collection using the GetCollection method. Populate the ComponentsInPackage collection, and enumerate through the collection to find the Bank.Account component. To associate the new role with the component, instantiate the RoleAssociationUtil object using the GetUtilInterface method. Then associate the new role with the component by calling the AssociateRole method.
    Set componentsInPack = packages.GetCollection("ComponentsInPackage", pack.Key)
                componentsInPack.Populate
                For Each comp In componentsInPack
                    If comp.Name = "Bank.Account" Then
                        Set rolesForComponent = componentsInPack.GetCollection("RolesForPackageComponent", comp.Key)
                        Set util = rolesForComponent.GetUtilInterface
                        util.associateRole (newRole.Key)
                        Exit For
                    End If
                Next
                Exit For
            End If
        Next
               
        Exit Sub
  7. Use the Err object to display an error message if the installation of the component fails.
    failed:
        MsgBox "Failure code " + Str$(Err.Number)
    
    End Sub

See Also

MTS Administration Objects, MTS Collection Types, MTS Administration Object Methods, Automating MTS Administration with Visual Basic