Copying custom commands

The following code sample copies a custom command, replacing all the Add object to group actions with the Remove object from group actions.

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

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

# Bind to the 'Custom Commands' container
$customCommandsPath = $service.Backend.GetConfigurationContainerPath(
    "CustomCommands")
$customCommandsContainer = $service.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 = $service.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)
}
C#
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 ns = new AdmNamespace();
        IAdmService service = ns.GetServiceDirectly("localhost");

        // Bind to the 'Custom Commands' container
        string customCommandsPath = service.Backend.GetConfigurationContainerPath(
            "CustomCommands");
        IADsContainer customCommandsContainer = (IADsContainer)service.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)service.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