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
$ns = New-Object "Softerra.Adaxes.Adsi.AdmNamespace"
$service = $ns.GetServiceDirectly("localhost")

# Bind to the 'Scheduled Tasks' container
$scheduledTasksPath = $service.Backend.GetConfigurationContainerPath(
    "ScheduledTasks")
$scheduledTasksContainer = $service.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 = $service.OpenObject("Adaxes://$ouDN", $null, $null, 0)
$moveAction.TargetContainer = $targetOU
$action.SetAction($moveAction)
$action.SetInfo()
$actionsAndConditions.Actions.Add($action)

$task.ConditionedActions.Add($actionsAndConditions)

See also