Hello,
I am trying to figure out how to create a scheduled task via PowerShell.
I've been referencing the SDK, mostly pulling script examples straight out of it. After following the steps on http://www.adaxes.com/sdk/ManagingScheduledTasks.html I can successfully create a task, with a schedule.
However, when trying to follow http://www.adaxes.com/sdk/DefiningActio ... tions.html to add actions and conditions, I consistently get the same error: You cannot call a method on a null-valued expression. at the same line which reads: $actionsAndConditions = $task.ConditionedActions.Create()
The actions/conditions I copied out of your SDK.
Help???
Full Script
[Reflection.Assembly]::LoadWithPartialName("Softerra.Adaxes.Adsi")
# Connect to the Adaxes service
$admNS = New-Object "Softerra.Adaxes.Adsi.AdmNamespace"
$admService = $admNS.GetServiceDirectly("localhost")
# Bind to the 'Scheduled Tasks' container
$scheduledTasksPath = $admService.Backend.GetConfigurationContainerPath(
"ScheduledTasks")
# Build the ADS path of the child container 'My Container'
$scheduledTasksPathObj = New-Object "Softerra.Adaxes.Adsi.AdsPath"`
$scheduledTasksPath
$myContainerAdsPath = $scheduledTasksPathObj.CreateChildPath("CN=O365ScriptScheduled")
$myContainer = $admService.OpenObject($myContainerAdsPath, $NULL, $NULL, 0)
# Create a new Scheduled Task
$task = $myContainer.Create("adm-ScheduledTask", "CN=My Tasks12")
$task.ObjectType = "user"
$task.Description = "My description"
$task.Disabled = $False
$task.ExecutionMoment = "ADM_BUSINESSRULEEXECMOMENT_BEFORE"
$task.OperationType = "none"
# The $task variable refers to a Scheduled Task
$recurrencePattern = $task.GetRecurrencePattern()
$recurrencePattern.RecurrenceType = "ADM_RECURRENCEPATTERNTYPE_ONCE"
$recurrencePattern.PatternStartDateTime = (Get-Date).AddDays(30)
$task.SetRecurrencePattern($recurrencePattern)
$task.DeleteTaskAfterExecution = $True
#======================================================>
# The $obj variable refers to a Business Rule, Custom Command or Scheduled Task
# Create a new set of actions and conditions
$actionsAndConditions = $task.ConditionedActions.Create()
$actionsAndConditions.ConditionsLogicalOperation = "ADM_LOGICALOPERATION_AND"
$actionsAndConditions.SetInfo()
#-----------------------------------------------------------------------
# If located under the 'Distribution Lists' OU
$condition = $actionsAndConditions.Conditions.CreateEx("adm-LocationCondition")
$locationCondition = $condition.GetCondition()
$locationCondition.IsOperator = "ADM_ISOPERATOR_IS"
$locationCondition.Scope = "ADS_SCOPE_SUBTREE"
$ouDN = "OU=Distribution Lists,DC=domain,DC=com"
$ou = $admService.OpenObject("Adaxes://$ouDN", $NULL, $NULL, 0)
$locationCondition.Container = $ou
$condition.SetCondition($locationCondition)
$condition.SetInfo()
$actionsAndConditions.Conditions.Add($condition)
#-----------------------------------------------------------------------
# Establish e-mail address for the Group (Alias: '%samAccountName%')
$action = $actionsAndConditions.Actions.CreateEx("adm-ExchangeTaskAction")
$action.ExecutionOptions = "ADM_ACTIONEXECUTIONOPTIONS_SYNC"
$mailEnableAction = $action.GetAction()
$mailEnableAction.TaskType = "ADM_EXCHANGETASKTYPE_MAILENABLE"
$mailEnableAction.MailAlias = "%samAccountName%"
$action.SetAction($mailEnableAction)
$action.SetInfo()
$actionsAndConditions.Actions.Add($action)
# Add the set to the Business Rule, Custom Command or Scheduled Task
$task.ConditionedActions.Add($actionsAndConditions)
#======================================================>
# Save the Scheduled Task
$task.SetInfo()