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 (40 points)

1 Answer

0 votes
by (11.1k 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 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)
0 votes
1 answer

Hi All, I am currently using the 30 day free trial of Adaxes and seeing if we can use it to achieve our method of user provisioning. I am looking into server-side ... variable value within an SQL query Can this be achieved? Any help is much appreciated, Thanks

asked Feb 1 by Lewis (40 points)
3,351 questions
3,052 answers
7,791 comments
545,083 users