Script Repository


Schedule a task after removing an object

November 24, 2015
1021

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.

Parameters:

  • $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
PowerShell
$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 = "recipient@domain.com" # 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)
{
    try
    {
        $task = $Context.BindToObject($taskPath)
        return $False
    }
    catch
    {
        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)
    {
        break
    }
    $uniqueName = "$taskName`_$i"
}

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

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

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

$task.SetInfo()

# Define actions and conditions

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

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

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

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

$task.ActivityScopeItems.Add($scopeItem)


Comments ( 0 )
No results found.
Leave a comment