Skip to content

Creating Scheduled Tasks

The following code sample creates a Scheduled Task. The task will move users between Organizational Units based on the user’s department.

[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 'Scheduled Tasks' container
$scheduledTasksPath = $admService.Backend.GetConfigurationContainerPath(
    "ScheduledTasks")
$scheduledTasksContainer = $admService.OpenObject($scheduledTasksPath,
    $NULL, $NULL, 0)

# Create new Scheduled Task
$task = $scheduledTasksContainer.Create("adm-ScheduledTask", "CN=My Task")

$task.ObjectType = "user"
$task.Description = "My description"
$task.Disabled = $False
$task.ExecutionMoment = "ADM_BUSINESSRULEEXECMOMENT_BEFORE"
$task.OperationType = "none"

# Specify the schedule for the task
$recurrencePattern = $task.GetRecurrencePattern()
$recurrencePattern.RecurrenceType = "ADM_RECURRENCEPATTERNTYPE_DAILY"
$recurrencePattern.Interval = 1
$now = Get-Date
$recurrencePattern.PatternStartDateTime = Get-Date `
    -year $now.Year `
    -month $now.Month `
    -day $now.Day `
    -hour 4 `
    -minute 0 `
    -second 0
$recurrencePattern.NotBeforeDateTime = $now
$task.SetRecurrencePattern($recurrencePattern)
$task.SetInfo()

# Specify actions and conditions for the task

$actionsAndConditions = $task.ConditionedActions.Create()
$actionsAndConditions.ConditionsLogicalOperation =
    "ADM_LOGICALOPERATION_AND"
$actionsAndConditions.SetInfo()

# If the Department property equals 'Sales'
$condition = $actionsAndConditions.Conditions.CreateEx(
    "adm-AttributeOperatorValueCondition")
$conditionObj = $condition.GetCondition()
$conditionObj.AttributeName = "department"
$conditionObj.ComparisonOperator = "ADM_COMPARISONOPERATOR_EQUAL"
$value = New-Object "Softerra.Adaxes.Adsi.AdsPropertyValue"
$value.PutObjectProperty("ADSTYPE_UNKNOWN", "Sales")
$conditionObj.Value = $value
$conditionObj.CaseSensitive = $False
$condition.SetCondition($conditionObj)
$condition.SetInfo()
$actionsAndConditions.Conditions.Add($condition)

# Move the user to the 'Sales' OU
$action = $actionsAndConditions.Actions.CreateEx("adm-CopyMoveAction")
$action.ExecutionOptions = "ADM_ACTIONEXECUTIONOPTIONS_SYNC"
$moveAction = $action.GetAction()
$moveAction.Move = $True
$ouDN = "OU=Sales,DC=domain,DC=com"
$targetOU = $admService.OpenObject("Adaxes://$ouDN", $NULL, $NULL, 0)
$moveAction.TargetContainer = $targetOU
$action.SetAction($moveAction)
$action.SetInfo()
$actionsAndConditions.Actions.Add($action)

$task.ConditionedActions.Add($actionsAndConditions)

See also