Skip to content

Copying Custom Commands

The following code sample copies a Custom Command, replacing all ‘Add object to group’ actions with ‘Remove object from group’ actions.

[Reflection.Assembly]::LoadWithPartialName("Softerra.Adaxes.Adsi")

# Connect to the Adaxes service
$admNS = New-Object "Softerra.Adaxes.Adsi.AdmNamespace"
$admService = $admNS.GetServiceDirectly("localhost")

# Bind to the 'Custom Commands' container
$customCommandsPath = $admService.Backend.GetConfigurationContainerPath(
    "CustomCommands")
$customCommandsContainer = $admService.OpenObject($customCommandsPath,
    $NULL, $NULL, 0)

# Bind to the source Custom Command
$commandsPathObj = New-Object "Softerra.Adaxes.Adsi.AdsPath" $customCommandsPath
$sourceCommandPath = $commandsPathObj.CreateChildPath("CN=Source Command")
$sourceCommand = $admService.OpenObject($sourceCommandPath, $NULL, $NULL, 0)

# Copy the Custom Command
$newCommandRdn = "CN=My Command"
$newCommand = $customCommandsContainer.CopyHere($sourceCommandPath, $newCommandRdn)
$newCommand.SetInfo()

# Copy actions and conditions
foreach ($set in $sourceCommand.ConditionedActions)
{
    # Create a new set of actions and conditions
    $actionsAndConditions = $newCommand.ConditionedActions.Create()
    $actionsAndConditions.ConditionsLogicalOperation = 
        $set.ConditionsLogicalOperation
    $actionsAndConditions.SetInfo()

    # Copy conditions
    foreach ($condition in $set.Conditions)
    {
        $newCondition = $actionsAndConditions.Conditions.CreateEx($condition.Class)
        $newCondition.SetCondition($condition.GetCondition())
        $newCondition.SetInfo()
        $actionsAndConditions.Conditions.Add($newCondition)
    }

    # Copy actions
    foreach ($action in $set.Actions)
    {
        $newAction = $actionsAndConditions.Conditions.CreateEx($action.Class)
        $newAction.ExecutionOptions = $action.ExecutionOptions
        $actionObj = $action.GetAction()

        # 'add to group' -> 'remove from group'
        if ($actionObj.IsOperationOfType($NULL, "change membership") -and
            $actionObj.ActionType -eq "ADM_CHANGEGROUPMEMBERSHIPACTION_ADD")
        {
            $actionObj.ActionType = "ADM_CHANGEGROUPMEMBERSHIPACTION_REMOVE"
        }
        $newAction.SetAction($actionObj)
        $newAction.SetInfo()
        $actionsAndConditions.Actions.Add($newAction)
    }

    # Add the set to the Custom Command
    $newCommand.ConditionedActions.Add($actionsAndConditions)
}
using System;
using Softerra.Adaxes.Adsi;
using Softerra.Adaxes.Interop.Adsi;
using Softerra.Adaxes.Interop.Adsi.BusinessRules;
using Softerra.Adaxes.Interop.Adsi.CustomCommands;
using Softerra.Adaxes.Interop.Adsi.PersistentObjects;
class Program
{
    static void Main(string[] args)
    {
        // Connect to the Adaxes service
        AdmNamespace adsNS = new AdmNamespace();
        IAdmService admService = adsNS.GetServiceDirectly("localhost");

        // Bind to the 'Custom Commands' container
        String customCommandsPath = admService.Backend.GetConfigurationContainerPath(
            "CustomCommands");
        IADsContainer customCommandsContainer = (IADsContainer)admService.OpenObject(
            customCommandsPath, null, null, 0);

        // Bind to the source Custom Command
        AdsPath commandsPathObj = new AdsPath(customCommandsPath);
        AdsPath sourceCommandPath = commandsPathObj.CreateChildPath("CN=Source Command");
        IAdmCustomCommand sourceCommand =
            (IAdmCustomCommand)admService.OpenObject(
            sourceCommandPath.ToString(), null, null, 0);

        // Copy the Custom Command
        const String newCommandRdn = "CN=My Command";
        IAdmCustomCommand newCommand =
            (IAdmCustomCommand)customCommandsContainer.CopyHere(
            sourceCommandPath.ToString(), newCommandRdn);
        newCommand.SetInfo();

        // Copy actions and conditions
        foreach (IAdmBusinessRuleConditionedActions set in sourceCommand.ConditionedActions)
        {
            // Create a new set of actions and conditions
            IAdmBusinessRuleConditionedActions actionsAndConditions =
                (IAdmBusinessRuleConditionedActions)newCommand.ConditionedActions.Create();
            actionsAndConditions.ConditionsLogicalOperation = 
                set.ConditionsLogicalOperation;
            actionsAndConditions.SetInfo();

            // Copy conditions
            foreach (IAdmBusinessRuleCondition condition in set.Conditions)
            {
                IAdmBusinessRuleCondition newCondition =
                    (IAdmBusinessRuleCondition)actionsAndConditions.Conditions.CreateEx(
                    condition.Class);
                newCondition.SetCondition(condition.GetCondition());
                newCondition.SetInfo();
                actionsAndConditions.Conditions.Add(newCondition);
            }

            // Copy actions
            foreach (IAdmBusinessRuleAction action in set.Actions)
            {
                IAdmBusinessRuleAction newAction = 
                    (IAdmBusinessRuleAction)actionsAndConditions.Conditions.CreateEx(
                    action.Class);
                newAction.ExecutionOptions = action.ExecutionOptions;
                IAdmAction actionObj = action.GetAction();

                // 'add to group' -> 'remove from group'
                if (actionObj.IsOperationOfType(null, "change membership"))
                {
                    IAdmChangeGroupMembershipAction removeFromGroupAction =
                        (IAdmChangeGroupMembershipAction)actionObj;
                    if (removeFromGroupAction.ActionType == 
                        ADM_CHANGEGROUPMEMBERSHIPACTION_ENUM.ADM_CHANGEGROUPMEMBERSHIPACTION_ADD)
                    {
                        removeFromGroupAction.ActionType =
                            ADM_CHANGEGROUPMEMBERSHIPACTION_ENUM.ADM_CHANGEGROUPMEMBERSHIPACTION_REMOVE;
                    }
                }
                newAction.SetAction(actionObj);
                newAction.SetInfo();
                actionsAndConditions.Actions.Add(newAction);
            }
            // Add the new set to the Custom Command
            newCommand.ConditionedActions.Add(actionsAndConditions);
        }
    }
}

See also