The official PASS Blog is where you’ll find the latest blog posts from PASS community members and the PASS Board. Contributors share their thoughts and discuss a wide variety of topics spanning PASS and the data community.

WorkFlows – What Are They? – Part 2

Workflows Unmasked – Restarting Workflows

One of the uses of workflows is to be able to restart them as needed. In the beginning of previous article, it was mentioned that workflows can be used for “Interruptible activities that can be stopped and re-started”. In this article, we will see the stop and restart feature. To see it in action, let’s bring back our Basic_Workflow program, a good candidate for this because it does a lot of things with only a little code: connects to databases, saves data in files, compresses data, and emails reports.

Here is the code of the BasiC_Workflow_Suspended.ps1, very similar to the original Basic_Workflow.ps1. A condition was added to suspend the workflow if any of the files could not be produced (zero length file).

Let’s prepare conditions – first, we save our files into the work folder C:\WORKFLOWS:

  • Basic_Workflow_Suspended.ps1
  • Basic_Workflow_CodeBlocks.ps1
  • Basic_Workflow.cmd

 

Then, we will have the SQL Server ready with the 4 test databases:

  • AdventureWorks2008R2
  • AdventureWorks2008R2_A
  • AdventureWorks2008R2_B
  • AdventureWorks2008R2_C

 

To recap, the program will bring data from the four databases as csv files, the files are compressed, then an email report is sent.

From the command line, we execute the Basic_Workflow.cmd:

Next, we create a condition for stopping the workflow – one of the databases has an incorrect name (added X to the name) – and then we run it again.

Now, we are in a limbo status; our program is completed and we know that something should be going on somewhere. It’s time to get back to our enlightenment training (see Part 1). We spoke about jobs, it was said that workflows are the ultimate modern thing, but to resume the workflow you need the jobs paraphernalia.

Here is a reminder of the Jobs basics:

Now, we check in a PowerShell session what jobs are running. Let’s open a PowerShell command window and execute “Get-Job”. This is what we receive:

And... we realize that while working on workflows we were creating jobs! Notice the State is “Suspended”.

Note to aspiring politicians reading this, you can use this for your campaigns.

Now comes the interesting part.

 

Resuming Workflows for the Rest of Us

There are two ways to resume the workflow:

  1. The first one is to dig into the documentation of both jobs and workflows, learn the many switches there, and use them as appropriate. Been there, done that, total confusion.
  2. The second one is to use this line of code within another PowerShell script, let’s name it ResumeWorkflow.ps1.
  • # SAVE THIS LINE: Get-Job -State Suspended | Resume-Job -Wait| Wait-Job
  • Wrap it in a cmd file like this one, conveniently named ResumeWorkflow.cmd.
  • Powershell C:\WORKFLOWS\ResumeWorkFlow.ps1 > C:\WORKFLOWS\ResumeWorkFlow.out
  • And execute it.

Now, our stopped workflow can be resumed.

Notice the change of State to “Completed”.

 

Stopping and Resuming Workflows – The How To

  1. In your workflow script, place the call to “Suspend-Workflow” in the right place for the condition needed. You will soon discover that you cannot place the condition inside your trusty InlineScripts, it has to be out in the Orange zone (refer to Part 1).
  2. Have your ResumeWorflow.cmd file ready.
  3. Execute your workflow.
  4. If needed, execute your ResumeWorkflow.cmd.

Note here: there is no issue if you execute your ResumeWorkflow twice or more times, no harm done, see the output (no jobs shown).

Time after resuming workflow: 2/14/2020 3:17:12 PM

Thanks for reading, I hope this helps in your PowerShell workflows endeavors.

 

Links

All source code here.

Snover’s talk about programming with hand grenades (near the 5:30 time). This is a must see for any PowerShell wannabe.

Keep this link handy, you will need it.

Interesting video for you – PowerShell used in WWII.

Jorge Besada
About the author

Jorge Besada is a SQL Server DBA with more than 15 years’ experience as a DBA. An electronic engineer in a previous life with an MSEE degree, Jorge morphed into an Information Technology professional and is enjoying every minute of it. His main interest is in automation of all tasks using PowerShell and Python, switching between them as needed.

Please login or register to post comments.

Theme picker

Back to Top
cage-aids
cage-aids
cage-aids
cage-aids