Make New ActiveBatch Job References Using PowerShell

Posted by:

|

On:

|

ActiveBatch is a workload automation solution that is extremely powerful when utilized properly. It’s intuitive to work in and can be very powerful with well-defined processes. ActiveBatch has one or more “job schedulers” that schedule and queue jobs, and connect to a database to store job properties, including schedules, variables, steps, etc. An “execution agent” actually runs the job once it’s passed from the Scheduler. Essentially, we talk to the scheduler which talks to everything else for us. It’s possible to make execution agents out of virtual machine scale sets and increase the availability of ActiveBatch, or to load balance between agents.

Unfortunately, it can be difficult to find ActiveBatch documentation. The knowledge base has no organizational structure, and there are dozens of PDFs your answer could be in, not considering the helpfiles. Such is the case with many enterprise applications.

I’ve worked in places where there are many repeatable steps that are ripe for automation, but work would be created from scratch as needed countless times. There’s the added issue of the ActiveBatch team not knowing the use case and doing exactly what is requested, rather than what may be the optimal solution.

There are a handful of ways you can make a job: through the GUI, using API, or using PowerShell. Specifically, it’s possible to leverage the ActiveBatch COM to create references to jobs and insert variables, to make sure all processes are uniform. I spent some time reading the documentation and trying to hit different URIs on the server would always get 501 when trying to use the API.

If you combine this script with something like Microsoft Forms and Power Automate, you could allow end-users to create their own automation workloads just by entering a few variables into the form, assuming you have golden templates.

PowerShell
# Required Variables:
#    $jobSchedulerHostName,
#    $newReferenceName,
#    $jobPath 
#
# Requires elevated permisisons
# Requires connectivity to $jobSchedulerHostName

# Connect to Job Scheduler and put information into the new reference object.
$jobScheduler = New-Object -ComObject "ActiveBatch.AbatJobScheduler"
$jobScheduler.Connect("JobSchedulerHostName")
$newReference = $jobScheduler.CreateObject("Reference")
$newReference.Label = $newReferenceName
$newReference.Name = $NewReferenceName
$newReference.TargetJobID = 12345 # This has to be an integer, not a PowerShell object

# Create the reference.
$jobScheduler.AddObject($jobPath, $newReference)

# Add variable to reference plan.
$oJob = $jobScheduler.getabatobject($newReference.ID)
$Variables = $oJob.GetVariables()
$Variables.SetVariable($variableName, $Description, $variableValue, $true) # True creates job if not exist
$oJob.Update()