I have a PowerShell Script (being run in a Custom Command) that creates a Scheduled Task that runs another Custom Command but I want the resulting Scheduled Task to delete itself once it has run. Essentially, I'm creating a schedulable Custom Command workflow that's fire-and-forget.
What is the safest way to do this? I don't want to rely on a batch job to clear out said Tasks from a Container.
Some example code I have been playing with:
$UserDisplayName = "%displayName%"
$UserObjectDN = "%distinguishedName%"
$ScheduledDate = [datetime]::ParseExact("%param-ScheduledDateTime%","dd/MM/yyyy HH:mm:ss",$null)
$TasksContainerDN = "CN=Once-Off,CN=Scheduled Tasks,CN=Configuration Objects,CN=Adaxes Configuration,CN=Adaxes" #Or Perhaps $Context.GetWellKnownContainerPath("ScheduledTasks")
$CustomCommandID = "{00000000-0000-0000-0000-000000000000}"
$CustomCommandDN = "CN=My Custom Command,CN=Custom Commands,CN=Configuration Objects,CN=Adaxes Configuration,CN=Adaxes"
$CustomCommandParamValues = @{
    MyParamOne = "%param-MyParamOne%"
    MyParamTwo = "%param-MyParamTwo%"
}
$scheduledTasksContainer = $Context.BindToObject($TasksContainerDN)
$scheduledTasksContainer.Filter = @("adm-ScheduledTask")
$existingTaskFound = $false
foreach($scheduledTask in $scheduledTasksContainer)
{
    if($scheduledTask.TaskName -eq "My Custom Command - $($UserDisplayName) %objectGUID%") {
        $existingTaskFound = $true
        $Context.Cancel("$($UserDisplayName) is already scheduled for My Custom Command.")
    }
}
if(!$existingTaskFound) {
    $task = $scheduledTasksContainer.Create("adm-ScheduledTask", "CN=My Custom Command - $($UserDisplayName) %objectGUID%")
    $task.ObjectType = "user"
    $task.Description = "My Custom Command - $($UserDisplayName) %objectGUID% (Created by %adm-InitiatorFullName%)"
    $task.Disabled = $False
    $task.ExecutionMoment = "ADM_BUSINESSRULEEXECMOMENT_BEFORE"
    $task.OperationType = "none"
    $recurrencePattern = $task.GetRecurrencePattern()
    $recurrencePattern.RecurrenceType = "ADM_RECURRENCEPATTERNTYPE_ONCE"
    $recurrencePattern.PatternStartDateTime = $ScheduledDate
    $task.SetRecurrencePattern($recurrencePattern)
    $task.DeleteTaskAfterExecution = $True
    $task.SetInfo()
    $actionsAndConditions = $task.ConditionedActions.Create()
    $actionsAndConditions.ConditionsLogicalOperation = "ADM_LOGICALOPERATION_AND"
    $actionsAndConditions.SetInfo()
    $action = $actionsAndConditions.Actions.CreateEx("adm-CustomCommandAction")
    $action.ExecutionOptions = "ADM_ACTIONEXECUTIONOPTIONS_SYNC"
    $customCommand = $Context.BindToObjectByDN($CustomCommandDN)
    $arguments = $customCommand.CreateArguments()
    foreach($Parameter in $CustomCommandParamValues.Keys) {
        $arguments.SetParameterValue($Parameter, $CustomCommandParamValues.Item($Parameter))
    }
    $customCommandAction = $action.GetAction()
    $customCommandAction.CustomCommandId = $CustomCommandID
    $customCommandAction.Arguments = $arguments
    $action.SetAction($customCommandAction)
    $action.SetInfo()
    $actionsAndConditions.Actions.Add($action)
    $task.ConditionedActions.Add($actionsAndConditions)
    $scopeItem = $task.ActivityScopeItems.Create()
    $scopeItem.BaseObject = $Context.BindToObject("Adaxes://$($UserObjectDN)")
    $scopeItem.Type = "ADM_SCOPEBASEOBJECTTYPE_CONTAINER "
    $scopeItem.Inheritance = "ADS_SCOPE_BASE"
    $scopeItem.Exclude = $False
    $scopeItem.SetInfo()
    $task.ActivityScopeItems.Add($scopeItem)
    $task.SetInfo()
}