PowerShell 3.0 Workflow

Getting Started with Workflow in PowerShell 3.0PowerShell 3.0 Workflow

This technique for understanding PowerShell’s Workflow is very much my own making.  It is designed to illustrate the three keywords ‘Workflow, Parallel and Sequence’.

Note 1: You need PowerShell version 3 or later.

 ♦

Example 1: Simple PowerShell v3 Workflow

The only point of this basic example is to demonstrate how Workflow operates with a cmdlet or ‘wrapper’.  Once I grasped this cmdlet / wrapper idea it was relatively straightforward to add ‘Sequence’ and ‘Parallel’ sections to my workflow.

# Most Basic PowerShell Workflow Example
Workflow Verb-Noun {
"Stuff!"
}
Clear-Host
Verb-Noun

Important: Nothing happens if you omit the last line: Verb-Noun

Expected result merely:
Stuff!

Example 2a: To Create a Workflow Called Guy-ParaSeq

The underlying PowerShell task here is to execute Get-Process and then Get-Service, but padded with a few literal lines of "In x of y".  In truth, this is an example to learn about workflow rather than performing any useful tasks.

From a Workflow perspective, take the time to trace the three parallel items, and the two sequence items both in the script and in the results.

Principle: Get this example working, then experiment by re-ordering the elements.

# PowerShell 3.0 Workflow Example
Workflow Guy-ParaSeq {
# Commands within Parallel execute in any order
Parallel {
"In parallel 1 of 3"
   Sequence {
   "In sequence 1 of 2"
   Get-Process -Name Power*
   "In sequence 2 of 2"
                   } # Closes Sequence
"In parallel 2 of 3"
Get-Service -Name W32Time
"In parallel 3 of 3"
   }  # Closes Parallel
}  # Closes Guy-ParaSeq cmdlet

Clear-Host
Guy-ParaSeq

Note 2:  Observe how ‘In sequence 2 of 2’ has to wait for Get-Process to finish, however, ‘In parallel 2 of 3’ does not have to wait.

Note 3: It’s crucial to run Guy-ParaSeq on the last line.

Example 2b: Difference Sequence for Get-Process -Name Power*

# Windows PowerShell Workflow Example
Workflow Guy-ParaSeq1 {
Parallel {
"In parallel 1 of 3"
Get-Process -Name Power*
   Sequence {
   "In sequence 1 of 2"
   "In sequence 2 of 2"
                   }
"In parallel 2 of 3"
Get-Service -Name W32Time
"In parallel 3 of 3"
   }  # Closes Parallel
}  # Closes cmdlet

Clear-Host
Guy-ParaSeq1

Note 4: I hope that these two examples will encourage you to experiment with the order of Parallel and Sequence items.

Engineer's Toolset v10Guy Recommends: SolarWinds Engineer’s Toolset v10

This Engineer’s Toolset v10 provides a comprehensive console of 50 utilities for troubleshooting computer problems.  Guy says it helps me monitor what’s occurring on the network, and each tool teaches me more about how the underlying system operates.

There are so many good gadgets; it’s like having free rein of a sweetshop.  Thankfully the utilities are displayed logically: monitoring, network discovery, diagnostic, and Cisco tools.  Try the SolarWinds Engineer’s Toolset now!

Download your fully functional trial copy of the Engineer’s Toolset v10

Example 3: PowerShell Workflow with InlineScript

A number of ordinary PowerShell commands give errors in workflow, the way around these difficulties is to employ InlineScript.

Workflow Guy-ParaSeqInline {
Parallel { Get-Process -Name Svchost*
Sequence {
      "In sequence 1 of 2"
       InlineScript {
       Start-Sleep 10
                  }
       "In sequence 2 of 2"
                 } # Closes Sequence
"In parallel 1 of 2"
"In parallel 2 of 2"
InlineScript {
$a = 9
$b = $a+2
$b
                 }
Get-Service -Name W32Time
    }  # Closes Parallel
}

Clear-Host
Guy-ParaSeqInline

Note 5:  Observe how this script employs Start-Sleep to emphasise the parallel and sequence portions, if you don’t see what I mean then experiment by moving the commands around and pay attention to ‘In parallel x of y’.

Workflow Background

Good project management takes a task and breaks it down into steps, each with resources and a time-line.  Workflow charts display which tasks can be tackled simultaneously, for instance fuelling cars at a big gas (petrol) station, and which are constrained by the need for activities to complete in sequence, for example, an insurance claim form.

Workflow in PowerShell
In script performance terms we can make gains by identifying which jobs can be executed in parallel.  This pre-supposes that we are comfortable with PowerShell’s jobs, a factor which rules out PowerShell novices.

See more tasks for PowerShell »

Summary of PowerShell's Workflow

 I designed this page to illustrate the three keywords ‘Workflow, Parallel and Sequence’.  Please remember that Workflow is only available in PowerShell v 3.0 and later.

If you like this page then please share it with your friends

 


See more Microsoft PowerShell v 3.0

PowerShell Tutorials  • What’s New in PowerShell 3.0  • PowerShell 3.0 Foreach-Object

PowerShell Show-Command  • Out-GridView -PassThru  • PowerShell Ordered Hash Tables

PowerShell Version 3.0  • PowerShell 3.0 Get-ChildItem  • PowerShell 3.0 Update-Help