0 votes

Using the powershell module, I know how to create a scheduled task, and also how to bind to a scheduled task that is already known. I also have used code to try creating a scheduled task which will re-name it if one already exists by the same name. However, all of these depend on using a try-catch function to pull an error based on a known full name of the task.

I have a use case where I want to get a list of all scheduled tasks that have a name that begins with specific text, but the ending may be different. For example, all tasks where the name is like "My Task ID" (using as wildcard).

Once I get the list of those tasks, I need to loop through them and check the start times. All of this is done so that I can create a task that does not run at the same time as another. These are all one-time tasks and will be removed once executed.

by (60 points)

1 Answer

0 votes
by (14.5k points)

Hello,

The below script can be used to find scheduled tasks whose names start with a certain string and output the task names. In the script, the $searchString variable specifies the string for search. The script can be executed in Windows PowerShell on the computer where the Adaxes service runs.

[Reflection.Assembly]::LoadWithPartialName("Softerra.Adaxes.Adsi")

$searchString = "My Task ID" # TODO: modify me

# Get the scheduled task container parh
$ns = New-Object "Softerra.Adaxes.Adsi.AdmNamespace"
$service = $ns.GetServiceDirectly("localhost")

# Build criteria
$criteria = New-AdmCriteria -Type "adm-ScheduledTask" -Expression {name -startswith $searchString}

# Search options
$scheduledTasksContainerPath = $service.Backend.GetConfigurationContainerPath("ScheduledTasks")
$searcher = $service.OpenObject($scheduledTasksContainerPath, $NULL, $NULL, 0)
$searcher.SearchScope = "ADS_SCOPE_SUBTREE"
$searcher.Criteria = $criteria

try
{
    # Execute search
    $searchResultIterator = $searcher.ExecuteSearch()
    $searchResults = $searchResultIterator.FetchAll()
}
finally
{
    # Release resources
    if ($searchResultIterator) { $searchResultIterator.Dispose() }
}

if ($searchResults.Length -eq 0)
{
    # No tasks found
    Write-Host "No tasks were found"
    return
}

foreach ($searchResult in $searchResults)
{
    # Bind to the task
    $task = $service.OpenObject($searchResult.AdsPath, $NULL, $NULL, 0)

    # Get task name
    $taskName = $task.Get("name")

    # Output task name
    Write-Host $taskName
}

To get a schedule of a task, the GetRecurrencePattern method of the IAdmScheduledTask interface can be used. For more details on how to manage scheduled tasks, have a look at the following SDK article: https://www.adaxes.com/sdk/ManagingScheduledTasks.

How to search based on Activity Scope

Related questions

0 votes
1 answer

The script create two reports of inactive workstation operating systems. The report is too detailed to run from one of the adaxes reports. Basically how can I set the script up to ... sure How I did this but I can't find it now (probably something simple).

asked Nov 30, 2022 by mightycabal (1.0k 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

I'm in the process of creating a Web interface for requesting IT accounts. Upon submission, I want to run a Powershell script that will create an item in a Sharepoint task list.

asked May 14, 2021 by sandramnc (870 points)
0 votes
1 answer

I need to send an e-mail to the owner ("managed by") for each group. The e-mail should contain a list of group members. What is the best way to do that?

asked May 9 by akindy (20 points)
0 votes
1 answer

I have an ADP Sync scheduled task that modifies and creates users from a csv file. I also have reports that show new users created and management history for user ... ADP Sync scheduled task so that they only run after the ADP Sync task is complete?

asked Jan 7, 2020 by barberk (60 points)
3,564 questions
3,255 answers
8,264 comments
547,915 users