Skip to content

Replacing actions and conditions of a Scheduled Task

The following code sample replaces all actions and conditions of a Scheduled Task with new ones.

[Reflection.Assembly]::LoadWithPartialName("Softerra.Adaxes.Adsi")

# Connect to the Adaxes service
$admNS = New-Object "Softerra.Adaxes.Adsi.AdmNamespace"
$admService = $admNS.GetServiceDirectly("localhost")

# Bind to 'My Task' Scheduled Task
$scheduledTasksPath = $admService.Backend.GetConfigurationContainerPath(
    "ScheduledTasks")
$scheduledTasksPathObj = New-Object "Softerra.Adaxes.Adsi.AdsPath" `
    $scheduledTasksPath
$myScheduledTaskAdsPath = $scheduledTasksPathObj.CreateChildPath( `
    "CN=My Task")
$myScheduledTask = $admService.OpenObject($myScheduledTaskAdsPath, $NULL, $NULL, 0)

# Delete old actions and conditions
$myScheduledTask.ConditionedActions.Clear()

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

# If account expired more than 30 days ago
$condition = $actionsAndConditions.Conditions.CreateEx(
    "adm-AccountPasswordExpirationCondition")
$expirationCondition = $condition.GetCondition()
$expirationCondition.ExpirationType = "ADM_EXPIRATIONTYPE_ACCOUNT"
$expirationCondition.ExpirationOperator =
    "ADM_EXPIRATIONOPERATOR_EXPIRED"
$expirationCondition.PeriodOperator =
    "ADM_EXPIRATIONCOMPARISONOPERATOR_MORETHAN"
$expirationCondition.PeriodValue = 30
$condition.SetCondition($expirationCondition)
$condition.SetInfo()
$actionsAndConditions.Conditions.Add($condition)

# Delete the object
$action = $actionsAndConditions.Actions.CreateEx(
    "adm-DeleteObjectAction")
$action.ExecutionOptions = "ADM_ACTIONEXECUTIONOPTIONS_SYNC"
$deleteAction = $action.GetAction()
$deleteAction.DeleteSubtree = $True
$action.SetAction($deleteAction)
$action.SetInfo()
$actionsAndConditions.Actions.Add($action)

# Add the set to the Scheduled Task
$myScheduledTask.ConditionedActions.Add($actionsAndConditions)

See also