Skip to content

Creating Custom Commands

The following code sample creates a Custom Command. The command will add users to a group and send an email notification.

[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)

# Create new Custom Command
$command = $customCommandsContainer.Create("adm-CustomCommand", "CN=My Command")

$command.ObjectType = "user"
$command.Confirmation = "Are you sure?"
$command.CommandIcon = 7

$command.Description = "My description"
$command.Disabled = $False

$command.ExecutionMoment = "ADM_BUSINESSRULEEXECMOMENT_BEFORE"
$command.OperationType = "none"

# Save the Custom Command
$command.SetInfo()

# Create a new set of actions and conditions
$actionsAndConditions = $command.ConditionedActions.Create()
$actionsAndConditions.ConditionsLogicalOperation =
    "ADM_LOGICALOPERATION_AND"
$actionsAndConditions.SetInfo()

# Add the user to the 'SalesGroup' group
$action = $actionsAndConditions.Actions.CreateEx(
    "adm-ChangeGroupMembershipAction")
$action.ExecutionOptions = "ADM_ACTIONEXECUTIONOPTIONS_SYNC"
$addToGroupAction = $action.GetAction()
$addToGroupAction.ActionType =
    "ADM_CHANGEGROUPMEMBERSHIPACTION_ADD"
$groupDN = "CN=My Group,DC=example,DC=com"
$group = $admService.OpenObject("Adaxes://$groupDN", $NULL, $NULL, 0)
$addToGroupAction.Group = $group
$action.SetAction($addToGroupAction)
$action.SetInfo()
$actionsAndConditions.Actions.Add($action)

# Send email notification
$action = $actionsAndConditions.Actions.CreateEx(
    "adm-SendMailNotificationAction")
$action.ExecutionOptions = "ADM_ACTIONEXECUTIONOPTIONS_SYNC"
$sendMailAction = $action.GetAction()
$sendMailAction.RecipientAddresses = "%mail%"
$sendMailAction.Subject = "%username%, your group membership has changed"
$sendMailAction.Text = "You've been added to group " + $group.Get("cn")
$action.SetAction($sendMailAction)
$action.SetInfo()
$actionsAndConditions.Actions.Add($action)

# Add the set to the Custom Command
$command.ConditionedActions.Add($actionsAndConditions)

See also