Orchestrate your scripts with workflows – Part 2
As we saw in first part of this posts serie. Workflows are very convenients, but they lack things to be the true killer in PowerShell. The first missing thing is a real way to control your workflow flow !
Control a worklow execution
If we speak about orchestration it’s important to consider when your worflow can be suspended, or backup variables when a computer/server reboots. Differents options exist:
- Checkpoint-Workflow : Where you can stop & resume the workflow
Workflow Test-CheckPoint { InlineScript { "Echo A" Sleep 5 "Echo End A" } Checkpoint-Workflow InlineScript { "Echo B" Sleep 5 "Echo End B" } Checkpoint-Workflow "Echo Tadaaaahhh!" }
$Job = Test-CheckPoint -AsJob Suspend-Job $Job Receive-Job $Job
Resume-Job $Job
Receive-Job $Job
Note that if you really want to suspens the workflow, you must use Suspend-Job -Force
- $PSPersistencePreference is used to indicate if your workflow can be suspended and/or resumed after every command
Workflow Test-CheckPoint { $PSPersistPreference = $true InlineScript { "Echo A" Sleep 5 "Echo End A" } InlineScript { "Echo B" Sleep 5 "Echo End B" } "Echo Tadaaaahhh!" }
Now you can control the execution of your workflows like you do with your jobs, but one thing is missing here… How debug workflows, because it’s kinda cloudy here 😉
Debug workflows
The first thing to know when you want to debug your workflows is to use Write-Output instead of Write-Host if you want something to be traced.
Workflow Test-Workflow { InlineScript { "Echo A" Sleep 5 "Echo End A" } InlineScript { "Echo B" Sleep 5 "Echo End B" } "Echo Tadaaaahhh!" } $Job = Test-Workflow -AsJob
Once your workflow has started, let’s look at what is happening on it.
$job.ChildJobs[0].progress
If we had an error in our workflow, i can get the error like this
$job.ChildJobs[0].Error
You can have detailled information about each inlinescript, but how about going deeper in the debugging without using PowerShell jobs ?
Enable-PSWSManCombinedTrace Test-Workflow Disable-PSWSManCombinedTrace
We now have a beautiful trace in $pshome\traces\PSTrace.etl
Get-WinEvent -Path C:\Windows\System32\WindowsPowerShell\v1.0\Traces\PSTrace.etl -Oldest
You should now be able to diagnostic your workflows even if they are already in production.
This is all regarding workflows on this blog, but if you want to learn more about them have a look to “man about_workflowCommonParameters” on your PowerShell console, you’ll be surprised with all hidden gems.