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

# Bind to the scheduled task
$scheduledTasksPath = $service.Backend.GetConfigurationContainerPath(
    "ScheduledTasks")
$scheduledTasksPathObj = New-Object "Softerra.Adaxes.Adsi.AdsPath" `
    $scheduledTasksPath
$myScheduledTaskAdsPath = $scheduledTasksPathObj.CreateChildPath( `
    "CN=My Task")
$myScheduledTask = $service.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