Skip to content

Creating Business Rules

The following code sample creates a Business Rule. The rule will create Exchange mailboxes and home folders for newly created users in Active Directory.

[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 'Business Rules' container
$businessRulesPath = $admService.Backend.GetConfigurationContainerPath(
    "BusinessRules")
$businessRulesContainer = $admService.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 store
$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 = $admService.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)

Tip

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

See also