Summary
This sample shows how to use PowerShell and the adTempus API to search adTempus jobs for a string and display the results.
Script Code
Save the following code as "adt-search.ps1":
param (
[string]$server = ".",
[string]$find = $(throw "-find is required."),
[switch]$details = $false
)
add-type -path "c:\program files\arcana development\adtempus\4.0\ArcanaDevelopment.adTempus.Client.dll"
$adtempus=[ArcanaDevelopment.adTempus.Client.Scheduler]::Connect($server,[ArcanaDevelopment.adTempus.Shared.LoginAuthenticationType]::Windows,"","")
$context=$adtempus.NewDataContext()
$options=new-object ArcanaDevelopment.adTempus.Shared.ObjectSearchOptions
$options.Action=[ArcanaDevelopment.adTempus.Shared.SearchReplaceType]::SearchOnly
$options.ReturnObjects=$true
$options.IncludeObjects.Add([ArcanaDevelopment.adTempus.Shared.WellKnownOIDs]::RootGroup) #Search the root job group and all its contents
$options.SearchSubGroups=$true #including sub-groups
$options.TextToFind=$find #look for the specified text
$results=$null #this will hold the results
$messages=$null #this would hold error messages if we were doing a replace
$context.SearchAndReplace($options,[ref] $results,[ref] $messages)
foreach($result in $results)
{
write-host ("Job " + $result.GetPrimaryObject().FullyQualifiedName)
if($details)
{
foreach($match in $results.Matches)
{
write-host ($match.FindLocation + " > " + $match.FieldDisplayName)
}
write-host "**********"
}
}
Usage
The script must be run under a user account that has permission to connect to the adTempus server and view jobs.
To find all jobs containing the text "my text" on the local adTempus server:
.\adt-search -find "my text"
To find all jobs containing the text "my text" on adTempus server "remoteserver":
.\adt-search -find "my text" -server "myserver"
To show details of where the match was found within the job, include the -details option:
.\adt-search -find "my text" -details
Adapting the Code
This sample lists only the name of each job where the text is found, but can be adapted to show additional information. The $result variable is a SearchReplaceResult. $result.Matches is a collection of SearchReplaceMatch objects, each of which contains information about where the text was found within the job.
References
- API reference for DataContext.SearchAndReplace