Automation of jobs with dependencies

Hi everyone,

Apologies if this has been asked before—I may not have been using the best search terms.

We’re using PSU, specifically the Automation/Scripts feature, to set up a process where scripts can run sequentially and depend on the success or failure of previous scripts. This is a big improvement for us, as we currently just run scripts on fixed schedules.

Here’s the workflow we’re aiming for:

  • At 10 PM, run test-process1.
  • If test-process1 fails, stop. Otherwise, move to the next step.
  • Run test-process2.
  • If test-process2 fails, stop. Otherwise, move to the next step.
  • Run test-process3.
  • Done.

From what I’ve found so far, the only way to achieve this is by creating a single PowerShell script to handle the entire workflow and scheduling it for 10 PM.
Below is what we cooked up.

Before I go down that path, I just wanted to check if PSU has any built-in features that could simplify this process.

Thanks in advance for any guidance!

Import-Module -Name Universal

$ErrorActionPreference = "Stop"

$AppToken = "sometoken"

try {
    $Script = Get-PSUScript -Name 'test-process1.ps1' -AppToken $AppToken 
    $execJob = Invoke-PSUScript -Script $Script -AppToken $AppToken 
    Wait-PSUJob -Job $execJob -AppToken $AppToken
    $execJob = Get-PSUJob -Id $execJob.Id -AppToken $AppToken
    
    If ($execJob.Status -ne "Completed") {
        Throw "test-process0: Error in test-process1"
    } Else {
        $Script = Get-PSUScript -Name 'test-process2.ps1' -AppToken $AppToken
        $execJob = Invoke-PSUScript -Script $Script -AppToken $AppToken 
        Wait-PSUJob -Job $execJob -AppToken $AppToken
        $execJob = Get-PSUJob -Id $execJob.Id -AppToken $AppToken
    } 
   
    If ($execJob.Status -ne "Completed") {
        Throw "test-process0: Error in test-process2"
    } Else {
        $Script = Get-PSUScript -Name 'test-process3.ps1' -AppToken $AppToken
        $execJob = Invoke-PSUScript -Script $Script -AppToken $AppToken 
        Wait-PSUJob -Job $execJob -AppToken $AppToken
        $execJob = Get-PSUJob -Id $execJob.Id -AppToken $AppToken
    } 

    If ($execJob.Status -ne "Completed") {
        Throw "test-process0: Error in test-process3"
    }

} catch {    
    $errmsg = $_.Exception.Message    
    throw $errmsg
}
Product: PowerShell Universal
Version: 5.1.2

There isn’t something specific like this built into PSU. That said, it has been brought up in the past and we have an open issue for it here: Workflows · Issue #1393 · ironmansoftware/powershell-universal · GitHub

Honestly, I hadn’t realized how desired this was (based on the upvotes on the issue) and we should schedule this for an upcoming release.

2 Likes