Changing task schedule

The following code sample shows how to change the schedule of a scheduled task.

[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)

# Set the task schedule to once every day at 4:00am
$recurrencePattern = $myScheduledTask.GetRecurrencePattern()
$recurrencePattern.RecurrenceType = "ADM_RECURRENCEPATTERNTYPE_DAILY"
$recurrencePattern.Interval = 1
$now = Get-Date
$recurrencePattern.PatternStartDateTime = Get-Date `
    -year $now.Year `
    -month $now.Month `
    -day $now.Day `
    -hour 4 `
    -minute 0 `
    -second 0
$recurrencePattern.NotBeforeDateTime = $now
$myScheduledTask.SetRecurrencePattern($recurrencePattern)

# Save changes
$myScheduledTask.SetInfo()

See also