Creating business rules

The following code sample creates a business rule. The rule will create Exchange mailboxes and home folders for newly created users.

[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 'Business Rules' container
$businessRulesPath = $service.Backend.GetConfigurationContainerPath(
    "BusinessRules")
$businessRulesContainer = $service.OpenObject($businessRulesPath,
     $null, $null, 0)

# Create new business rule
$rule = $businessRulesContainer.Create("adm-BusinessRule", "CN=My Rule")

$rule.ExecutionMoment = "ADM_BUSINESSRULEEXECMOMENT_AFTER"
$rule.ObjectType = "user"
$rule.OperationType = "create"

$rule.Description = "My description"
$rule.Disabled = $false

# Save the business rule
$rule.SetInfo()

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

# If the operation succeeded
$condition = $actionsAndConditions.Conditions.CreateEx(
    "adm-OperationResultCondition")
$operationResultCondition = $condition.GetCondition()
$operationResultCondition.OperationResult =
    "ADM_OPERATIONRESULT_SUCCEEDED"
$condition.SetCondition($operationResultCondition)
$condition.SetInfo()
$actionsAndConditions.Conditions.Add($condition)

# Create Exchange mailbox
$action = $actionsAndConditions.Actions.CreateEx(
    "adm-ExchangeTaskAction")
$action.ExecutionOptions = "ADM_ACTIONEXECUTIONOPTIONS_SYNC"
$createMailboxAction = $action.GetAction()
$createMailboxAction.TaskType = "ADM_EXCHANGETASKTYPE_CREATEMAILBOX"
# Set the alias
$createMailboxAction.MailAlias = "%username%"
# Set the mailbox database
$mailboxStorageDN = 
    "CN=MyDatabase,CN=Databases,CN=Exchange Administrative Group," +
    "CN=Administrative Groups,CN=First Organization,CN=Microsoft Exchange," +
    "CN=Services,CN=Configuration,DC=domain,DC=com"
$mailboxStore = $service.OpenObject("Adaxes://$mailboxStorageDN",
    $null, $null, 0)
$createMailboxAction.MailboxStorageDatabase = $mailboxStore
# Set the mailbox selection type
$createMailboxAction.MailboxStorageSelectionType =
    "ADM_MAILBOXSTORAGESELECTIONTYPE_NONE"
$action.SetAction($createMailboxAction)
$action.SetInfo()
$actionsAndConditions.Actions.Add($action)

# Create home folder
$action = $actionsAndConditions.Actions.CreateEx(
    "adm-CreateHomeDirectoryAction")
$action.ExecutionOptions = "ADM_ACTIONEXECUTIONOPTIONS_SYNC"
$createHomeDirAction = $action.GetAction()
$createHomeDirAction.HomeDirectory = "\\SERVER\SHARE\%username%"
$createHomeDirAction.HomeDrive = "Z:"
$createHomeDirAction.SetUserAsOwner = $false
$createHomeDirAction.UserAccessPermissions =
    "ADM_USERACCESSPERMISSION_FULL"
$createHomeDirAction.InheritParentPermissions = $true
$action.SetAction($createHomeDirAction)
$action.SetInfo()
$actionsAndConditions.Actions.Add($action)

# Add the set to the business rule
$rule.ConditionedActions.Add($actionsAndConditions)

For details on how to get the DN of a mailbox database, see Get the DN of a mailbox database.

See also