Script Repository

Schedule a task after removing an object

March 17, 2021

The script can be used to schedule a task that will be executed several days after removing an AD object and referring to properties of the deleted object. For this purpose, the script creates a scheduled task and passes the properties of the deleted object to that task. You can use the script in a business rule triggered after deleting an object of the type you need. For example, if you want to schedule tasks after deleting users, create a business rule triggered after deleting a user and add this script using the Run a program or PowerShell script action.


  • $daysAfterDeletion - Specifies the number of days after object deletion when the task will run.
  • $containerName - Specifies the name of the container where the rule will create Scheduled Tasks.
  • $taskName - Specifies a pattern for scheduled task names.
  • $taskDescription - Specifies a description for the scheduled tasks.
  • $deleteTaskAfterExecution - Specifies whether to delete the tasks after execution. We recommend deleting the tasks as they will accumulate with the course of time.
  • $scriptToExecute - Specifies a script that will be executed by the scheduled task.
  • $scriptDescription - Specifies a description of the script executed by the scheduled task.
Edit Remove
$daysAfterDeletion = 90 # TODO: modify me

# Scheduled task settings
$containerName = "My Container" # TODO: modify me
$taskName = "ScheduledTask - %username%" # TODO: modify me
$taskDescription = "My description" # TODO: modify me
$deleteTaskAfterExecution = $False # TODO: modify me

# Script for action
$scriptDescription = "My Script" # TODO: modify me
$scriptToExecute = {
$to = "" # TODO: modify me
$subject = "Information on the user removed 90 days ago"
$message = "
Username: %username%
User description: %description%" # TODO: modify me
$Context.SendMail($to, $subject, $message, $NULL)

function CheckNameForUnique($taskPath)
        $task = $Context.BindToObject($taskPath)
        return $False
        return $True

# Bind to the scheduled tasks container
$scheduledTasksPath = $Context.GetWellKnownContainerPath("ScheduledTasks")
$scheduledTasksPathObj = New-Object "Softerra.Adaxes.Adsi.AdsPath" $scheduledTasksPath
$containerPath = $scheduledTasksPathObj.CreateChildPath("CN=$containerName")
$container = $Context.BindToObject($containerPath)

# If the task name is not unique, generate a unique one
$uniqueName = $taskName
for ($i = 1; $True; $i++)
    $taskPath = $containerPath.CreateChildPath("CN=$uniqueName")
    if (CheckNameForUnique $taskPath)
    $uniqueName = "$taskName`_$i"

# Create a scheduled task
$task = $container.Create("adm-ScheduledTask", "CN=$uniqueName")

$task.ObjectType = "domainDNS"
$task.Description = $taskDescription
$task.Disabled = $False
$task.OperationType = "none"
$task.DeleteTaskAfterExecution = $deleteTaskAfterExecution

$recurrencePattern = $task.GetRecurrencePattern()
$recurrencePattern.RecurrenceType = "ADM_RECURRENCEPATTERNTYPE_ONCE"
$recurrencePattern.PatternStartDateTime = (Get-Date).AddDays($daysAfterDeletion)


# Define actions and conditions

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

# Add Run PowerShell Script action
$action = $actionsAndConditions.Actions.CreateEx("adm-RunScriptAction")
$scriptAction = $action.GetAction()
$scriptAction.ScriptType = "ADM_SCRIPTTYPE_POWERSHELL"
$scriptAction.ScriptDescription = $scriptDescription
$scriptAction.Script = $scriptToExecute.ToString()

# Add the set to the scheduled task

# Set the scope of activity to All Objects
$scopeItem = $task.ActivityScopeItems.Create()
$scopeItem.BaseObject = $NULL
$scopeItem.Inheritance = "ADS_SCOPE_SUBTREE"
$scopeItem.Exclude = $False


Comments ( 0 )
No results found.
Leave a comment