0 votes

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()
by (710 points)

1 Answer

0 votes
by (216k points)
selected by
Best answer

Hello,

The error occurs because the Scheduled Task doesn't exist by the time when you are trying to create a new set of actions and conditions. To be able to create a new set of actions and conditions in a Scheduled Task, the task must be saved first. When you create a new Scheduled task by using the Create method and set some properties of the task, such as description, schedule etc, you are only putting the properties of the new task in the property cache without saving anything. Thus, before creating a set of actions and conditions, you need to call the IADs::SetInfo method to save your Scheduled Task. If you look at the examples in Managing Scheduled Tasks more attentively, all of them call the IADs::SetInfo method right after defining a schedule for a task.

That is, in your case, you need to insert the following line before the line that fails:
$task.SetInfo()

0

This worked perfectly, thank you.

I will point out however, that after going back and looking... attentively, I still see no $task.SetInfo() line after any of the script examples in the section labeled "defining a schedule", I see that line after each example of Creating a task, and modifying a task, but not defining a schedule.

Related questions

0 votes
1 answer

Hi, I need to start Adaxes scheduled task from Powershell console running on another host. How can I do that?

asked May 21, 2020 by KIT (960 points)
0 votes
1 answer

I have a scheduled task that runs a Powershell script against an AD group, "Group 1". I need to get all of the members of Group 1, and add them to Group 2. The ... identity in the error message start with 'user;'? What is the correct way to accomplish this?

asked Aug 27, 2019 by ngb (290 points)
0 votes
1 answer

Trying to create a new group and getting the following error: "The name reference is invalid. (Server: example.com)" I've created groups before...the problem seems to have just ... a new Group object. It has me puzzled so I'm looking for some direction...

asked Feb 28, 2014 by RickWaukCo (320 points)
0 votes
1 answer

Currently, when I disable a user account in Adaxes, the group memberships of the user remain intact. I'd like to automate the removal of group memberships such as distribution ... a list of groups/DL that the user was previously in and removed from. Thanks!

asked Nov 3, 2021 by jayden.ang (20 points)
0 votes
1 answer

Hello - I'm working on my companies off boarding process and need to run a Custom Command that turns off access to different systems and resources at the ... -9612-c7c982baa49f}" $user.ExecuteCustomCommand($commandID) # Save the Scheduled Task $task.SetInfo()

asked Jul 16, 2015 by jakesomething (190 points)
3,546 questions
3,237 answers
8,230 comments
547,809 users