Using PowerShell to Submit Jobs based on File Triggers

Follow

A common requirement is to dynamically submit a Job that processes a file based on the file's status. This is simple, straight forward and reliable when there is only a single file arriving - configure a File Trigger Element on the Job that processes the file. However, in cases where there are multiple files arriving, the recommended approach is slightly different.

JAMS provides a PowerShell module available that can assist us with this scenario. The module contains cmdlet's that allow us to interact with our environment. Specifically, the cmdlet, Submit-JAMSEntry, is used to submit a Job to the schedule.

A JAMS Job can be created from the following PowerShell script. The script loops through a list of files and utilizes the cmdlet - Submit-JAMSEntry - to submit a JAMS Job for each of the files in the collection. A File Trigger Element can be defined on the Job to initiate it when the required files appear.

Be sure to update the script where noted.

 

#
#  Import the JAMS PowerShell module
#
Import-Module JAMS

#
#  Modify the path to match the defined File Trigger Element
#
$fileList = Get-ChildItem C:\Incoming\*.dat

# # Create an array where we will save the JAMS Entry number of job we submit#
#
$entryList = @()
# # Submit a JAMS Job to process each of the files # foreach ($file in $fileList) { # # get the filename from the file object # $filename = $file.FullName # # Update "ProcessFile" with the name of the JAMS Job. This job must have a parameter named "filename" # The "-UseVariables" parameter tells Submit-JAMSEntry to look for a PowerShell # Variable - defined above - that matches each parameter in the job and use the variable value # as the value for the parameter # $lastSubmit = Submit-JAMSEntry ProcessFile -UseVariables # # Write some information to the log file # write-host "Entry " $lastSubmit.JAMSEntry " submitted to process $filename" # Save the Entry number # $entryList += $lastSubmit.JAMSEntry } # # We have submitted a job to process each file, # now we will wait for all of them to complete. # Wait-JAMSEntry $entryList -verbose
Have more questions? Submit a request

Comments

  • Avatar
    David Puckett

    A sleep statement may be needed to delays loop iterations
    Sleep -Seconds 5

  • Avatar
    Jonathan Brune

    # The "-UseVariables" parameter tells Submit-JAMSEntry to look for a PowerShell
    # Variable - defined above

    Would be nice to have an example of this for newbies

  • Avatar
    Gennaro Piccolo

    Hello Jonathan, the example above is one such example. Submit-JAMSEntry is submitting a job called processFile and it has a parameter on it called filename.

    When the job is submitted it takes the value from the $filename variable in the Powershell script, and passes it to the processFile job parameter called filename which is submitted for each filename in the list of files in the text document.