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
$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)
# 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 = $service.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
- Managing custom commands
- Defining actions and conditions
- Binding to Adaxes-specific objects
- Managing Adaxes-specific objects
- IADs
- IADsContainer
- IAdmCustomCommand
- Online script repository